I was trying to find the row which contain the date same as my other file. But it has the error 91 on the line defining the row number. Anyone can help me to tackle this prob? The 'mth' was successfully found.
Sub abc()
Dim x As Workbook
Dim y As Workbook
Dim WB As Workbook
Dim PCFilePath As String
Dim PCFile As String
Dim RSFilePath As String
Dim RSFile As String
Dim mth As Date
Dim row_mth As Long
Dim Date1 As Date
Dim Date2 As Date
PCFilePath = Worksheets("Sheet1").Range("B2")
PCFile = Worksheets("Sheet1").Range("B3")
RSFilePath = Worksheets("Sheet1").Range("B8")
RSFile = Worksheets("Sheet1").Range("B9")
mth = Worksheets("Sheet1").Range("B5")
Workbooks.Open (PCFilePath & PCFile), UpdateLinks:=0
Set x = Workbooks.Open(PCFilePath & PCFile)
Workbooks.Open (RSFilePath & RSFile), UpdateLinks:=0
Set y = Workbooks.Open(RSFilePath & RSFile)
Set WB = ThisWorkbook
row_mth = y.Sheets("Sheet1").Range("B:B").Find(What:=mth, LookIn:=xlValues).Row
Dates can be tricky when using Range.Find It can be format dependent, and also the Date data type in VBA is not the same as on the worksheet. One is stored as a Double, the other as a VBA Date data type.
Instead try:
Dim mth as Double
or
.Find(What:=CDbl(mth), ...
Related
I have the below code which takes an XML file from a shared location and loads it into Excel. As soon as the file is opened, i get a "Run-time error '1004': Application-defined or object defined error" message.
Sub Load_XML()
Dim xml_file_path As String
Dim file_date As String
Worksheets("Start").Activate
file_date = Range("B1").Value
xml_file_path = "Y:\mydrive\" & file_date & "-000000_RREP1002.XML"
Workbooks.OpenXML Filename:= _
xml_file_path _
, LoadOption:=xlXmlLoadImportToList
Dim lstrow as Integer
Dim r as Range
lstrow = ActiveSheet.UsedRange.Rows.Count
Set r = Range("A2:AF & lstrow")
The code errors before the last line is executed, i.e. as soon as the OpenXML is done.
Any ideas please what could be causing this issue?
thanks!
I would refactor the code like below.
Public Sub Load_XML()
Dim wsStart As Worksheet
Set wsStart = ThisWorkbook.Worksheets("Start")
Dim file_date As String
file_date = wsStart.Range("B1").Value ' make sure all ranges have a worksheet specified!
Dim xml_file_path As String
xml_file_path = "Y:\mydrive\" & file_date & "-000000_RREP1002.XML"
Workbooks.OpenXML Filename:=xml_file_path, LoadOption:=xlXmlLoadImportToList
Dim lstRow As Long
lstRow = wsStart.UsedRange.Rows.Count
' I recommend a more reliable method to find the
Dim r As Range
Set r = wsStart.Range("A2:AF" & lstRow)
I recommend a more reliable method to find the last usted row than using
lstRow = wsStart.UsedRange.Rows.Count
It is better you use
lstRow = wsStart.Cells(wsStart.Rows.Count, "A").End(xlUp).Row
This will find the last used row in column A. Adjust the column to one that has always data and no blanks.
I have a function that gets the date from a certain cell in an excel sheet, splits the string that was in that cell using a space as a delimiter and keeps the first object in the array to store at as the date. However, I keep getting the following error message
"run-time error '91': Object variable or With block variable not set".
Any ideas of what might be wrong?
Public Function getDate(ws As Excel.Worksheet, row As Integer, col As Integer) As String
Dim inspDateTime As Variant
Dim dateArr() As String
Dim inspDateFinal As String
Set inspDateTime = ws.Cells(row, col)
dateArr = Split(inspDateTime, " ")
inspDateFinal = dateArr(0)
getDate = inspDateFinal
End Function
The following code is how I call the function:
Private Sub UploadData_Click()
Dim myWorkbook As Excel.Workbook
Dim myWorksheet As Excel.Worksheet
Dim inspDate As String
inspDate = getDate(myWorksheet, 10, 14)
' setting the workbook as the directory that was printed to the text box
Set myWorkbook = Workbooks.Open(Me.FilePathTxtBox)
' setting the first worksheet in the array of files entered
Set myWorksheet = myWorkbook.Worksheets(1)
End Sub
I want to see if the value in column A of one workbook is in column A of another workbook (and then return the row). However, I'm stuck at using match.
I tried different approaches with the match function, first with a reference, then I simply typed in a number to match. The problem is: As long as the current workbook (wbCurr) is open, it always looks for the value there. When I close it, it looks in the master file (wbMaster) which is what I want.
Sub ReportCreation()
Dim wbMaster As Workbook, wbCurr As Workbook As Workbook
Dim pathMaster As String, pathCurrent As String As String
Dim rowMaster As Variant
Dim rowCurrent As Long, lRowCurrent As Long, lRowMaster As Long
pathMaster = "C:\Users\VBA\report2019.xlsx"
pathCurrent = "C:\Users\VBA\report 042019.xlsx"
Set wbMaster = Workbooks.Open(pathMaster)
Set wbCurr = Workbooks.Open(pathCurrent) 'current month
' rowMaster = Application.Match(wbCurr.Worksheets(1).Cells(3, 1), wbMaster.Worksheets(1).Range("A:A"), 0)
rowMaster = Application.WorksheetFunction.Match(7, wbMaster.Worksheets(1).Range("A:A"), 0)
Debug.Print rowMaster
End Sub
I am trying to create a button on my Access form that takes the current entry and writes the information to an Excel doc
Public Sub ExportExcel()
Dim ObjEx As Object
Dim WhatIsThisVariableFor As Object
Dim Selec As Object
Dim Column As Integer
Dim Row As Integer
Dim CustName As String
Dim Org As String
Dim Contact As String
Dim Product As String
Dim Quantity As Integer
Dim rst As DAO.Recordset
Set ObjEx = CreateObject("EXCEL.APPLICATION")
Set WhatIsThisVariableFor = ObjEx.Workbooks.Add
'Set rst = CurrentDb.OpenRecordset("Select") <---- This will be used when I get the code working
Column = 1
Row = 1
CustName = "Test" '<---- This is only used for the test
Cells(Row, Column) = CustName
ObjEx.Visible = True
Set Selec = ObjEx.Selection
End Sub
This code creates the Excel doc, but leaves it blank. I think that the Cells(Row, Column) command isn't working because it would have to be called from within excel? I'm not sure (I am very new to VBA)
How can I write to the spreadsheet cells from within Access?
You need to qualify your Cells() function to Excel's application object.
ObjEx.ActiveSheet.Cells(Row, Column).Value = CustName
I would recommend that you also choose the worksheet object explicitly:
Dim ws As object
Set ws = ObjEx.Worksheets("Sheet1")
ws.Cells(row, column).value = CustName
I know this is a super generic error but I am new to VBA / Macros and cant get past this.
I have an and excel workbook that has data I need to copy to another excel workbook.
The excel workbook that the data is copied to is on a network share and will be written to frequently.
here is my macro code:
Sub export()
Dim exportFile As String
Dim importSheet As String
Dim rowData As String
exportFile = "\\<server>\spd\_Spec_ParaData\data_import.xlsx"
importSheet = "OutPutValues"
importRange = "A2:ZZ2"
' Get the row from the workbook that we are running in
rowData = Workbooks().Worksheets(importSheet).Range(importRange)
' Not sure if this will work, or always overwrite the last row. May need to be .Row+1
newRow = Workbooks(exportFile).Worksheets(1).Cells(Rows.Count, "A").End(xlUp).Row
exportRange = "A" & (newRow + 1) & ":ZZ" & (newRow + 1)
' Assuming Workbooks() gets the current workbook.
Workbooks(exportFile).Sheets(exportSheet).Range(exportRange) = Workbooks().Sheets(importSheet).Range(importRange)
End Sub
My error is poping up on the rowData=Workbooks(exportFile).Worksheets
Can someone help me figure out what I am doing wrong?
Thank you,
Jennifer
Try your code with the following modifications, I'm just opening the workbook and referencing the worksheet (I guess the problem is that). I'm closing the workbook straight after.
Sub export()
Dim exportFile As String
Dim importSheet As String
Dim rowData As String
Dim wb As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
exportFile = "\\<server>\spd\_Spec_ParaData\data_import.xlsx"
importSheet = "OutPutValues"
importRange = "A2:ZZ2"
'Open your workbook and point to your spreadsheet
Set wb = Workbooks.Open(exportFile)
Set ws1 = wb.Sheets(importSheet)
' Get the row from the workbook that we are running in
rowData = wb.ws.Range(importRange)
' Not sure if this will work, or always overwrite the last row. May need to be .Row+1
newRow = wb.Worksheets(1).Cells(Rows.Count, "A").End(xlUp).Row
exportRange = "A" & (newRow + 1) & ":ZZ" & (newRow + 1)
'NOTE: consider definying the variable "exportSheet", I will do it just for example purpose
Dim exportSheet As String: exportSheet = "InputValues"
Set ws2 = wb.Sheets(exportSheet)
' Assuming Workbooks() gets the current workbook.
wb.ws2.Range(exportRange) = wb.ws1.Range(importRange)
wb.Close
End Sub