VBA with Access and Excel Help

  • Thread starter x62350
  • Start date
  • Tags
    Excel
In summary, the conversation discusses a VBA problem in Access involving an Excel sheet. The goal is to identify unique IDs in column A of the Excel sheet and match them with the corresponding unique IDs in the Access table. The current issue is a type mismatch error on the variable uID, but it can be resolved by explicitly declaring uID as a string variable before the For loop.
  • #1
x62350
1
0
This is a VBA problem inside access involving an excel sheet.

I have an Access Table "sOrderTable" with [Unique ID], [Resource Group ID], [Priority] columns.

I am trying to Identify "Unique IDs" in Column A of the EXCEL SHEET and where they match [Unique ID] in the ACCESS TABLE matching and pasting[Resource Group ID] and [Priority] from ACCESS into columns AP and AQ in EXCEL.

The current error is 13 type mismatch on the row defining uID. I have tried labeling uID as String, Integer, Long and changing the range from A2:A to just A2

'Open Target File
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Open(sTargetCombine)
Set xlSheet = xlBook.Worksheets("Tracking")

'Select Data to copy paste and compare
Dim rID As Integer 'Resource ID
Dim pID As Integer 'Priority ID
Dim uID As String 'Unique Id


'Select First Column
xlSheet.Range("A1").Select
xlSheet.Range(Selection, Selection.End(xlDown)).Select
i = Selection.Rows.Count

For i = 2 To Rows.Count

uID = xlSheet.Range("A2:A" + Trim(Str(i))).Value
rID = DLookup("[Resource Group ID]", sOrderTable, "[Unique ID] =" & uID)
pID = DLookup("[Priority]", sOrderTable, "[Unique ID] =" & uID)

Next

xlSheet.Range("AP2" + Trim(Str(i))) = rID
xlSheet.Range("AQ2" + Trim(Str(i))) = pID
 
Physics news on Phys.org
  • #2
NextxlBook.Close SaveChanges:=TrueSet xlBook = NothingSet xlApp = NothingThe problem is that uID is not being recognized as a valid variable, it is throwing Type Mismatch Error (13).The solution is to modify the code so that uID is defined as a valid variable. To do this, you need to add a line of code that explicitly declares uID as a string variable. This can be done by adding the following line of code before the For loop: Dim uID As String
 

1. What is VBA and how is it used with Access and Excel?

VBA (Visual Basic for Applications) is a programming language that is used to automate tasks and create custom functions and procedures in Microsoft Office applications such as Access and Excel. VBA code can be written to manipulate data, format documents, and perform other tasks that are not possible with built-in functions and features.

2. How do I access the VBA editor in Access and Excel?

In both Access and Excel, you can access the VBA editor by pressing Alt + F11 on your keyboard. This will open the Visual Basic Editor, where you can view and edit your VBA code.

3. Can VBA be used with both Access and Excel at the same time?

Yes, VBA can be used with both Access and Excel simultaneously. You can write code that will interact with data and perform tasks between the two applications. For example, you can use VBA to pull data from an Access database and use it in an Excel spreadsheet.

4. Is VBA difficult to learn?

The difficulty of learning VBA depends on your level of programming experience. If you have no prior programming knowledge, it may take some time to understand the syntax and logic of VBA. However, there are many online resources and tutorials available to help you learn VBA and its applications in Access and Excel.

5. Can I record macros in VBA for Access and Excel?

Yes, both Access and Excel have a built-in macro recorder that can generate VBA code based on your actions. However, this code may not be optimized or fully functional, so it is recommended to learn how to write VBA code manually for more complex tasks.

Similar threads

  • Math Proof Training and Practice
6
Replies
175
Views
20K
Back
Top