First of all sorry for not including any code from my attempts as I don't even know where to begin to start trial coding. What Im trying to do is I have a sheet which I have pre formatted to be my report. I then use a macro to copy certain columns from one sheet to the report sheet. I need to report sheet to be singed by somebody in the business and therefore need the bottom few rows for that. My question is how do I get the pasted columns to paste onto the next sheet if they reach a line that I decided is the last line I would like any information on. I apologise for the very vague question so please ask questions if you need any more info.
Loop Horizontal page breaks. Something like this.
For x = 1 To .HPageBreaks.Count - 1
MsgBox .HPageBreaks(x).Location.Row
'Write your rows adding your "sheet" row value to the pagebreak rows.
ws.Range("D" & lRow + .HPageBreaks(x).Location.Row).Value = "SomeValue"
Next
Related
I have multiple sheets which I am looking to copy and paste the relevant cells from rows into a new worksheet. The defining factor is whether the rows are "Live" which is determined by whether today falls between the "start date" and "end date" columns. For all rows that are "live" certain cells on that row should then be copy pasted to the bottom of the list on the new worksheet. I would then repeat these steps for the other worksheets so that the new worksheet essentially contains a summary of all "live" lines from the other sheets.
I then ultimately need to create a button so I can re-run this at the end of each day.
Hoping you can help!
You've defined and explained what you want to do nicely. So the next step would be to start researching the actions you want the code to do:
Start with how to copy rows:
http://www.bluepecantraining.com/copying-moving-rows-columns-excel-vba/
Next is how to find the last used row in a worksheet:
https://powerspreadsheets.com/excel-vba-last-row/
Next is to apply your condition on what rows to copy:
Using VBA to check if a date is between two dates
Next is how to create a for loop that runs the code the desired number of times:
https://www.excel-easy.com/vba/loop.html (a tip is to use the LastRow that you defined via the second link i posted. eg: "For x = 1 To LastRow")
And lastly is how to assign a macro to a button or shape:
https://wellsr.com/vba/2017/excel/how-to-assign-a-macro-to-a-shape-in-excel/
Try to build a code with the help of these links. If you get stuck, come back and show us what you've created and where the problem is. I (aswell as and many others) would love to help you on from there.
I have a vba code that generates an email. I would like the subject to be the data from the first and last cells in my list. The thing is, my list isnt of a set length, sometimes it contains 5 pieces of data sometimes 8 etc. How do i tell vba to pick the first and last cell no matter the length of the list?
thanks
For me, best practice is to just have cells on your sheet that calculate the first and last row (different ways you can do that), then give those cells a range name such as FirstRow and LastRow. In your vba then you refer to these cells to make your code dynamic.
e.g:
firstRow = Range("FirstRow)
lastRow = Range("lastRow")
test = range(cells(firstRow,lastRow))
-- Note I have not written VBA in many many years so am writing the above from memory so it may be not be exact.
Of course you can do it all entirely in VBA using the xlDown method mentioned previously but I prefer the transparency of it being on the main page so that easily spot if something breaks.
Range("A1").End(xlDown).Value
Where the cell is where you want to start and the End part moves all the way to the end
I'm stuck on what should be a very simple question, which is frustrating me greatly.
Essentially, I'm sorting through many files and manipulating them, but am stuck on this: how do I add in a column with something like the date in every cell down to the end of where the other columns have data?
Say that there are 5 other columns with length of 20 each. What code would I use to make column 6 have a certain value with a length of 20? The length varies with each sheet I work on, and this has been the problem.
In a program like MATLAB, I could just denote this by putting something like F1:end, but finding the answer for Excel is driving me crazy!
Any help would be greatly appreciated.
You could programmatically do this in vb to detect the bottom row with a value in, though it sounds as though you might be after a more straight forwards working solution.
Type a value in the top row, and with the cell selected you should see a square in the bottom right of the cell, double click it to fill to the end of the used range.
In VBA you would typically look from the bottom up to find the last non-blank cell in a column. With this, you can get the .Row to concatenate into a Range object definition. Example:
with activesheet
.range("F1:F" & .cells(rows.count, 5).end(xlup).row) = Format(Now, "dd-mmm-yyyy hh:mm")
end with
This will fill column F from the first row to the cell in column F that is next to the last non-blank value in column E.
Sub Tester()
With ActiveSheet
.Range(.Range("E1"), _
.Cells(Rows.Count, "E").End(xlUp)).Offset(0, 1).Value = Date
End With
End Sub
I'm trying to write a VBA macro for Excel 2013. It's purpose is to merge two worksheets into a combined worksheet. (I tried to find a built in feature to do this but was unable to find what I needed).
What the macro needs to do is this:
Activate "Sheet3" and clear all rows starting with row 3 and down
Go into "Sheet1" and copy all rows starting with (A3:P3) and copy down until, and not including, the first row with null in the A column.
Go into "Sheet3" and paste those rows starting at A3.
Go into "Sheet2" and copy all rows starting with (A3:P3) and copy down until, and not including, the first row with null in the A column.
Go into "Sheet3" and past those rows starting at the first empty cell in column A.
I'm a novice at VBA but I've managed to find the following code and I'm trying to make it work to accomplish the above requirements.
Sub CreateCombinedSheet()
lastRow = ActiveSheet.Cells(65536, lastCol).End(xlUp).Row
ActiveSheet.Range("A3", ActiveSheet.Cells(lastRow, 12)).Copy
End Sub
I'm trying to write parts of it and test it as I go but I'm already getting a 1004 error with this:
Application-defined or object-defined error
Any thoughts on how I should work this?
Thanks
Activate "Sheet3" and clear all rows starting with row 3 and down
You do not need to activate a sheet to clear the rows. You may want to see THIS You can directly say
Sheets("BlahBlah").Rows("3:200").ClearContents
I have hardcoded 200 as an example. To find the end of rows, see the below point.
Go into "Sheet1" and copy all rows starting with (A3:P3) and copy down until, and not including, the first row with null in the A column.
Same for this. You do not need to go to that sheet. You need to first find the last row. I see that you are hardcoing the rows in your code. You don't need to do that. xl2007+ now has 1048576 rows. Use .Rows.Count Please see THIS
Go into "Sheet3" and paste those rows starting at A3.
To paste, you again don't need to go to that sheet. You can directly say
rng.Copy Sheet("BlahBlah").Rows(3)
Your point 4 and 5 are just variations of the above. I am sure you can now take it from here :). In case you still face any difficulty, simply post back.
This question is somewhat difficult to explain, so bear with me.
I am pulling data from a large table for my company and am trying to create a macro to make this data easier to read/understand. The data that is on the site changes every day based on what caused certain failures in our plant, which causes my macro to analyze data that isn't there or wrong cells (due to rows getting shifted/moved/added/removed). Because I don't think that was really clear, here is an example:
The macro says to select cells J5, J13, and J25. These were, when I was creating the macro, the values I wanted to be put in a list. However, when I pulled the data and ran the macro today, these values were in different spots on my sheet (the value for cell J13 is now in J12). This completely messes up all of the analysis and renders my macro / data pull useless.
Is there a way to have the macro select the data more intelligently? Perhaps have it check for the group name, then select the value from the cell next to it? I wish I could word this better... Thanks if you've gotten this far!
Simply put... yes. Here's a code exert for looking for a groupname and getting the adjacent cell:
Dim Group1Range As Range
'Look in ThisWorkbook
With ThisWorkbook
'Look in Sheet1
With .Sheets(1)
'Look in Column I
With .Columns("I:I")
'Find the text Group1
Set Group1Range = .Find(What:="Group1").Offset(0, 1)
End With
End With
End With
'Indicate the address of the found range
Debug.Print Group1Range.Address
End Sub
Now here are ways that you can improve your question:
Explain how you know that cell J13 is no longer valid, and that J12 is now.
Give us some sample data.
Give us your code.
Tell us what your end result would be, possibly with an example.