What I wish to do :
i. Locate an excel document using path and doc name √
ii. Obtain the last filled row’s number with a spreadsheet name without opening the file X
iii. Obtain the last filled row's number BEFORE a certain row number, X
ex: last filled row before row 40. There may be filled cells between 45-52,
but I would like the function to return say "32" and not "52" like it does for me
What I have:
The path to the file is a classic directory + file name, which are written in two separate cells in the spreadsheet that has the macro. It's irrelevant, just to show that File_Path = the path ...
File_Path = Chr(34) & Server & "\" & Range(Workbook_Loc).Value 'That's step one
In the file located at File_Path, I wish to go in the spreadsheet named MONTH YEAR (ex: File_Path("July 2015")) and find the last filled row's number. In the 3rd line, I try to fill a cell with the last row's value in B. I know there is a lot online for this, but I can't get it to work somehow...:
Set wb = Workbooks.Open(File_Path) ' This should open my workbook
lastRow = wb(Chr(34) & Month & " " & Year & Chr(34)).Cells(1000, col).End(xlUp).Row
Range(CurrentValue_Loc).Formula = "=INDEX('" & Server & "\" & Workbook & Month & " " & Year & "'!B" & lastRow & ",1)"
The previous code stops at the second line ; it opens the workbook located at File_Path and gives me the following error:
Run-time error '438'
Object doesn't support this property method
What am I doing wrong?
Finally, for my iii. point (see top example), I tried to do this:
lastRow = wb(Chr(34) & Month & " " & Year & Chr(34)).Cells(40, col).End(xlUp).Row
but it still gives me the last filled row, even if it is after row40
Thank you!
You do not have to type the name of the workbook again, when attempting to find the last rows. Just use the workbook variable, which you have already set.
Sub lastRow()
Dim lastRow As Long
lastRow = wb.Sheets(1).Cells(Rows.Count, 2).End(xlUp).Row
lastRow = wb.Sheets(1).Cells(40, 2).End(xlUp).Row
End Sub
Just use the variable you have already declared, and then the Sheets() method to define the sheet you're finding the last row on. In the example above we are finding the last row on the first worksheet in the workbook.
Related
I downloaded a macro from Get Digital Help:
Sub AddText()
Dim Lrow As Single
Lrow = Worksheets ("Sheet 1").Range("B" & Rows.Count).End(xlUp).Row+1
Worksheets("Sheet 1").Range("B & Lrow & ":C" & Lrow)=Worksheets("Sheet 1").Range("B3:C3").Value
This works for a single card. However I have numerous cards and need to select the appropriate card before using a macro like the above.
The information below may clarify the situation.
I am working on a set of stock control cards (68 individual cards) which have input columns "B" for the quantity sold and column "C" for the date sold.
The quantity and dates are to be picked from cells "P1" for the quantity and "Q1" for the date.
The stock card to be selected for the input comes from cell "R7". this cell gives the item number (i.e. the card number to have the entry made).
The existing macro below selects the correct stock card numbered in range "R7" and places the cursor on the first entry row in column "B".
I now need to enter the quantity sold and date in the next empty row down.
I cannot get the cursor to move beyond
Worksheets("Nail Cards").Range("C2:C4012").Find(Range("R7").Value.Select
The following last part of the macro is what I thought would input the data into the appropriate cells but does not appear to do anything.
Lrow=Worksheets("Nail Cards").Range("B & Rows.Count).End(xlUp).Row+1
Worksheets("Nail Cards").Range("B" & Lrow & ":C" & Lrow)=Worksheets ("Nail Cards").Range("P1:Q1).Value
End Sub
Using
Lrow = Worksheets("Nail Cards").Range("B" & Rows.Count).End(xlUp).Row + 1
Worksheets("Nail Cards").Range("B" & Lrow & ":C" & Lrow) = Worksheets("Nail Cards").Range("P1:Q1").Value
will insert the data from the range P1:Q1 on the sheet named "Nail Cards" to the next empty cells in columns B and C
I'm new to VBA(coding) i just need your help on a work that i'm doing. the process is on Sheet1 we have the raw data as you can see on the attachment
we have the names on column a & orders and column b. i just want to copy the orders which is on column b basing on the name from column a from sheet1 to sheet2 sample image on the attachment on the result
Also if we have a name on sheet1 that is not available in sheet2 we could just ignore or skip the order unless we added the name on sheet2. also the other condition is if we could skip a cell if it has values already we can only paste it if cells from column b on sheet2 are blank. like if john's order has been change on sheet1 from dumplings to soup. i want John to have the same first order he took earlier which is dumplings that is saved on sheet2 earlier unless i deleted the first order. hope you guys can help appreciate your time a lot.
this is would be my current code all i need is the code to skip cells with values in sheet2 on column b
Sub Rectangle1_Click()
Dim I, total, fRow As Integer
Dim found As Range
total = Sheets(2).Range("A" & Rows.Count).End(xlUp).Row
For I = 1 To total
answer1 = Worksheets(2).Range("A" & I).Value
Set found = Sheets(1).Columns("A:A").Find(what:=answer1)
If found Is Nothing Then
Worksheets(2).Range("A" & I).Value = ""
Else
fRow = Sheets(1).Columns("A:A").Find(what:=answer1).Row
Worksheets(2).Range("B" & I).Value = Worksheets(1).Range("B" & fRow).Value
End If
Next I
End Sub
I have a button that creates a table, (report generator)
in this table i link to various pages with the same syntax.
Worksheets("Engine").Range("E" & EngineStatus).Formula = "=" & newSheetName & "!I3"
It is working, and it is running on a loop.
However, when
I am linking the value from each sheet, in the same cell on each sheet.
but of course only for selected sheets.
The problem occur on the report, as seen on the screenshot.
Sheetname in this case is ACDTCM0137 and cell i3 is what i put in my code..
The output is it counts i as a increased number which it should not.
And it overwrites ALL rows in this column with it's LAST value..
So the last sheet might be called BDMETHR0148 and same cell..
But the last one in, is the one it shows for ALL rows.
How do i ensure that for each row it keeps the formatting from the cove above?
Meaning it should always bee by this syntax
You should read about relative and absolute reference in Excel.
Instead:
Worksheets("Engine").Range("E" & EngineStatus).Formula = _
"=" & newSheetName & "!I3"
Use:
Worksheets("Engine").Range("E" & EngineStatus).Formula = _
"=" & newSheetName & "!$I$3" ' dollar sign $ before column and row locks it
You wrote that you have button to create report, so maybe the code below will be even better (not recalculating until report generated again - values only, not formula):
' all vars before loop "dimmed" only once
(...)
Dim rngEngine As Range
Dim rngStatus As Range
' And in your loop
EngineStatus = ...
newSheetName = ...
Set rngEngine = Worksheets("Engine").Range("E" & EngineStatus)
Set rngStatus = Worksheets(newSheetName).Range("I3")
rngEngine.Value = rngStatus.Value
I have an excel document that is created from another deployment tool that reads in array values and creates the excel document. I'm trying to add the ability to loop through column b and find null value and merge cell a thru c.
The logic I would like to use is loop through column B and look for empty cells, get the cell row number and use that to determine where the value i want to merge and center is (i.e. server1 in the picture below). I would then merge and center that row and continue through the loop until I reach the end of the used range.
I have tried using the Range.FindNext(string value) property but was unsuccessful in getting it to work.
Example of current layout is as follows:
What I am shooting for:
You could loop through every row in the used range of the sheet, looking for rows with empty values ("") in Column B, then merging Columns A to C of that row. If you want to run it from PowerShell, you can create a new module in Excel, then open the excel file from PowerShell, run your module from PowerShell, then save. Hope that helps.
Public Sub Test()
Dim i As Long
Dim lastRow As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row 'Only loop through used rows
'skip row 1 since it has the headers
For i = 2 To lastRow
If Range("B" & i) = "" Then
Range("A" & i & ":C" & i).Merge
Range("A" & i & ":C" & i).HorizontalAlignment = xlCenter
End If
Next
End Sub
I have a list of sequential dates (1/01/2012, 2/01/2012, 3/01/2012 etc) in a column in Excel. I want Excel to check the current date and add that date to the bottom of the range if it isn't there already. I only want this to happen once per day so that there are no redundant entries.
For example:
If the list ends at 2/06/2013 and I open the workbook on 2/06/2013, nothing would happen. However, if I opened the workbook again the next day, on 3/06/2013, then that date would be added to the bottom of the list automatically.
I also have two formulas I need copied into the next two cells of that row. If a date was generated for A20, the formulas would be on B20 and C20. The cell references for year/month/date would need to increment by 1 (as in one row) for every new date entry.
For reference, the first formula is:
=SUMIF('Sheet1'!A:A,DATE(YEAR(A1),MONTH(A1),DAY(A1)),'Sheet1'!C:C)`
And the other formula is similar enough to be redundant for the point of solving this problem.
Thanks in advance.
Edit:
I worked out how to check the list and add a new date
Sub CheckDateAndEnter()
If Sheet10.Cells(Rows.Count, 1).End(xlUp).Value <> Date Then
Sheet10.Cells(Rows.Count, 1).End(xlUp)(2, 1) = Date
Sheet10.Cells(Rows.Count, 1).End(xlUp)(1, 2) = "=SUMIF('Sheet1'!A:A,DATE(YEAR(A304),MONTH(A304),DAY(A304)),'Sheet1'!C:C)"
End If
End Sub
however, those cell references in the formula need to increment once for a new column each time this occurs and I'm not sure how to implement that.
If you place this code into the "ThisWorkbook" Modules in the VBA Editor, and make sure you safe your file as a "macro-enabled workbook" it should work.
Hard-coding the formula here probably isn't the best method, and the way I've done it could be cleaner using the R1C1 notation.
Private Sub Workbook_Open()
Dim Sheet As Worksheet: Set Sheet = ThisWorkbook.Worksheets("Sheet1") ' Reference to your worksheet
Dim Entry As Range: Set Entry = Sheet.Cells(Sheet.Rows.Count, 1).End(xlUp) ' The Last Populated Cell in Column A
If IsEmpty(Entry) = True Then ' Optional, Used to populate the first cell
Entry.Value = Date
Entry.Offset(ColumnOffset:=1).Formula = "=SUMIF('Sheet1'!A:A,DATE(YEAR(A" & Entry.Row & "),MONTH(A" & Entry.Row & "),DAY(A" & Entry.Row & ")),'Sheet1'!C:C)` "
Exit Sub
End If
If Year(Entry) = Year(Date) Then
If Month(Entry) = Month(Date) Then
If Day(Entry) = Day(Date) Then
Exit Sub ' Last Entry = Today, Do Nothing!
End If
End If
End If
Set Entry = Entry.Offset(RowOffset:=1) ' Last Entry != Today, Goto Next Row and create Entry.
Entry.Value = Date
Entry.Offset(ColumnOffset:=1).Formula = "=SUMIF('Sheet1'!A:A,DATE(YEAR(A" & Entry.Row & "),MONTH(A" & Entry.Row & "),DAY(A" & Entry.Row & ")),'Sheet1'!C:C)` "
End Sub