I have looked at similar questions but they don't seem to fit my problem. I have two sheets in excel; one has a type of header/overview data for some tasks, the other has several lines of details for each task.
I need help writing a macro that will match the Task ID between the two sheets and copy the entire row of header data from sheet one into a blank row above the group of detail rows in sheet two.
(Before this code will run, I will run a macro that will insert a blank line between each group of detail data. When I get this working, I will combine the two pieces of code.)
The copy statement is where I am stuck. It returns Application defined or object-defined error.
Sub InsertMWITitles()
‘copies the MWI titles above the correct MWI Steps group
Dim lngLastRowSht1, lngLastRowSht2, counterSht1, counterSht2As Long
With Worksheets(“MWI Titles”)
lngLastRowSht1 = .Cells(.Rows.Count, 8).End(xlUp).Row
lngLastRowSht2 = Worksheets(“MWI Steps”).Cells(Worksheets(“MWI Steps”).Rows.Count, 1).End(xlUp).Row
For counterSht1 = 1 To lngLastRowSht1
For counterSht2 = 1 To lngLastRowSht2
‘if the Task ID in column H of the MWI Titles sheet matches the Element ID in column A of
‘the MWI Steps sheet, copy the entire title row to the steps sheet above the group
If Worksheets(“MWI Titles”).Range("H" & (counterSht1)).Value = Sheets(2).Range("A" & counterSht2).Value Then
Worksheets(“MWI Titles”).Range("A" & (counterSht2).EntireRow.Copy Sheets(“MWI Steps”).Range("A" & (counterSht2 – 1))
End If
Next counterSht2
Next counterSht1
End With
End Sub
I also tried to copy and insert with shift down, but couldn't get that to work either.
The top section of the sample data is sheet 1/MWI Titles (the header data) and the bottom section is sheet 2 /MWI Steps (the detail data).
Sample of data in both sheets
Related
I have an Excel workbook with multiple sheets, which are fed by data imported from multiple external excel files. To complete this action, I successfully built VBA code that allows the user to open the files.
Behind the scenes, one of the subs imports data from the General Ledger raw data into a spreadsheet with multiple columns. I am attaching a couple screen grabs here to show what is happening:
The pre-existing data looks like this:
Raw data once downloads will look like this:
Once the macro runs, the previous columns populate as intended, but because the last two columns are populated intermittently, they end up doing this:
I only summarized columns in these images, as the workbook has 28 columns preceding the two columns.
Due to the size of the data and the macro itself, I would like to maintain the structure of the code. Especially since it is intended that this macro be transferred to other general ledger workbooks.
My macro looks like this (summarized for time):
1. Sub Import_GL1001
2. Dim FileToOpen As Variant
3. Dim OpenBook As Workbook
4. Application.ScreenUpdating = False
5. FileToOpen=Application.GetOpenFileName(Title="Import_GL1001",FileFilter:="ExcelFiles (*xlsx*),*xlsx*")
6. If FileToOpen<> False Then
7. Set OpenBook=Application.Workbooks.Open(FileToOpen)
8. OpenBook.Sheets(1).Range("$A$2:$A$1500").Copy
9. ThisWorkbook.Worksheets("GL 1001.10").Range("A"&Rows.Count).End(xlUp).Offset(1,0).PasteSpecial xlPasteValues
10. OpenBook.Sheets(1).Range("$B$2:$B$1500").Copy
11. ThisWorkbook.Worksheets("GL 1001.10").Range("B"&Rows.Count).End(xlUp).Offset(1,0).PasteSpecialxlPasteValues
12. ......
13. OpenBook.Sheets(1).Range("$AC$2:AC$1500").Copy
14. ThisWorkbook.Worksheets("GL 1001.10").Range("AD"&Rows.Count).End(xlUp).Offset(1,0).PasteSpecial xlPastevalues
15. OpenBook.Sheets(1).Range("$AD$2:$AD$1500").Copy
15. ThisWorkbook.Worksheets("GL 1001.10").Range("AF"&Rows.Count).End(xlUp).Offset(1,0).PasteSpecialxlPasteValues
In an ideal world, the values would copy and paste as is, blanks and all, so when the macro is run in the future the two columns in question do not change position based on the last blank cell. I tried multiple methods and variations, but the only logical thing I could think of is if I manage to find a way to insert a "0" into each cell that is blank every time the data is imported, without changing all the blank cells (i.e. if we only have 30 rows of data, I don't want all of the blank cells in AF:AF to be "0"). If the cells have a value at all, then that means that the macro itself won't have to be dramatically retooled.
Please, try the next way:
The last row where to paste the values should be calculated only once, based on a column you know that it is all the time filled with values.
Dim lastERow As Long, sh As Worksheet
Set sh = ThisWorkbook.Worksheets("GL 1001.10")
lastERow = sh.Range("A" & sh.Rows.Count).End(xlUp).Offset(1,0).row 'last empty row
Then use this reference for all the columns where you intend pasting starting from the same empty cell row:
'your existing code
OpenBook.Sheets(1).Range("$A$2:$A$1500").Copy
sh.Range("A" & lastERow).PasteSpecial xlPasteValues
'...
'...
OpenBook.Sheets(1).Range("$AC$2:AC$1500").Copy
sh.Range("AD" & lastERow).PasteSpecial xlPastevalues
OpenBook.Sheets(1).Range("$AD$2:$AD$1500").Copy
sh.Range("AF" & lastERow).PasteSpecial xlPastevalues
In this way, the code will paste starting from the same empty row, in all columns.
If you want following your way of solving, please run the next code, but after your existing code runs. It will take the reference from the last filled cell of the A:A column:
Sub ZeroInEmptyCells()
Dim sh As Worksheet, lastRow As Long, rngEmpt As Range
Set sh = ThisWorkbook.Worksheets("GL 1001.10")
lastRow = sh.Range("A" & sh.rows.count).End(xlUp).row 'last row in column A:A
On Error Resume Next 'if no empty cells, the next code line will return an error (without this line...):
Set rngEmpt = Union(sh.Range("AD1:AD" & lastRow), sh.Range("AF1:AF" & lastRow)).SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
If Not rngEmpt Is Nothing Then rngEmpt.value = 0
End Sub
first of all I've read many topics but none has my solution.
I have a data of 4 columns and N Rows (as N is unknown number).
Here is a sample:
When a new row entered I want it to automatically create new sheet has the name which is written in title column of that row (for the first row, create sheet with name "M")
also copy all data in row M to the new sheet
i know there is a way
=<SheetName>!<cell> but it isn't really helps if i have for example 1000 columns,
i have to create 1000 sheet then copy 1000 times!!
that's all, thanks in advance.
also feel free to use any methods (such as VBA).
I think this could be messy especially if you end up with a lot of Data. The code below will allow you to run a Macro that will quickly create sheets based on the data. This macro will only work once but it does the job
Sub CopyRowsToSheet()
' Variables needed for the formula
Dim NofRows As Long
Dim i As Long
Dim iActive As Boolean
' Counting the number of Rows in the active sheet
With ActiveSheet
NofRows = .Range("A" & Rows.Count).End(xlUp).Row
' Cycling through the number of rows on the active sheet
' we have set i = 2 as there is a header on the first page. If there is no header then set i = 1
For i = 2 To NofRows
' Creating the new sheet
Worksheets.Add(, Sheets(Sheets.Count)).Name = "Row " & i
' Copy Data to new sheet
.Rows(i).Copy Sheets("Row " & i).Range("A1:D1")
Next i
End With
End Sub
Apologies not 100% sure how to make this automatic but if I find a way i will add it here for you
I have an Excel spreadsheet with over 60 columns. Each column contains data from a SharePoint survey. The column headers are the actual survey questions that have been imported from a SharePoint data connection. I'm trying to do several things here.
First, I want to copy each column to a new worksheet. (The reason I need to do this is so that I can add the data in each column to a PowerPivot Data Model. PowerPivot recognizes the entire worksheet as one table, so it won't let me select only "Column A" to add to the Data Model, it automatically adds the entire table with all 60 columns).
Since I don't want to manually add 60 new sheets first before this, I'd like the code to copy each column to a newly created worksheet. (the worksheets don't need to be named, I could do that manually, unless someone has an easy way to do this as well!)
Then, I'd like the code to loop through each column performing the copy and paste to the newly created sheet.
I've found some examples of the code here for several related topics, but I'm so new to VBA that I'm having a hard time putting it all together. Thank you all so much for your time!
I've tried the following which does copy it over, but I'm not sure how to add a new sheet and loop through the columns
Sub CopyColumnToNewSheet()
Dim lastRow As Long
lastRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
Sheets("Sheet2").Range("A1:A" & lastRow).Value = Sheets("Sheet1").Range("A1:A" & lastRow).Value
End Sub
You should do this instead, please note, there is no error handling, suit to your needs
Sub CopyColumnToNewSheet()
Const MySourcheSheetName = "Sheet1"
Dim CounterColumn As Long
For CounterColumn = 1 To Sheets(MySourcheSheetName).Cells.SpecialCells(xlCellTypeLastCell).Column
Sheets.Add
ActiveSheet.Name = Sheets(MySourcheSheetName).Cells(1, CounterColumn).Value 'ideally the title is unique and it's in the first row of each column
Sheets(MySourcheSheetName).Columns(CounterColumn).Copy Destination:=ActiveSheet.Columns(1)
Next CounterColumn
End Sub
Been searching around and got a bit confused.
I have a sheet (herein called "ModelSpec") filled with data of TVs with a header that starts at A5:Z5 (Example Header Title is "Part Number", "Brand", "Size", "Resolutions"...etc). Some are with data, some are blank cells.
I'd created some USERFORM to allow user to select how they want to search for their Model Specification.
What I'm trying to do now is to pick up a data ("Part Number") located in the USERFORM textbox that the user selected and do a search in the "ModelSpec" sheet for the Part Number (located in Column A6:A?). The number of rows of data may change and I guess we need a FOR loop to stop at empty rows.
Once found, the entire row will be copied and paste value only on A1:Z1 of the same ModelSpec sheet.
So basically I'm currently working on Private Sub within the FORMS and not MODULES.
From there I have another code to extract the details to a form else where.
Please help this confused man.
This is a starting point for you, and has all the elements you'd need to do what you are asking.
What's Happening:
Click event on the userForm launches this code
Get the last row of the sheet containing data
Loop through all rows
Compare the value of txtPartNumber to the value in Column A of the current row in the loop.
If a match is found, display message confirming and copy the row being searched to row 1 of the same sheet.
If no match found, display message.
notes:
There is no handling of user input error. No upper case to lower case, or dropdown box limiting the user's choices. A good thing to do (in another question) would be to create a dynamic drop down or comboBox, that is populated by already existing part numbers, limiting the user input error.
If there is more than one match on the page, it will loop through all of the rows and copy the match to row 1. Then copy over row 1 with any match found after that, so the LAST one will be the only one you see. If you want it to only match the FIRST, then include an End statement right after copying the search row to row 1. I'm assuming your search column has unique IDs / part numbers.
Code:
Private Sub cmdSearch_Click()
Dim lastRow As Long, lCol As Long, lRow As Long
Dim sName As String
sName = "ModelSpec"
lastRow = Sheets(sName).Range("A" & Rows.count).End(xlUp).row
For lRow = 6 To lastRow
'Check to see if A(lRow) = TextBox. Exact match required
If Sheets(sName).Cells(lRow, "A").Text = txtPartNumber.Text Then
MsgBox("Match Found for Part #: " & txtPartNumber.Text)
For lCol = 1 To 26 'Loop through columns A-Z, Copy lRow to Row 1
Sheets(sName).Cells(1, lCol) = Sheets(sName).Cells(lRow, lCol)
Next lCol
Else
MsgBox("No match found for Part #: " & txtPartNumber.Text)
End If
Next lRow
End Sub
I have two worksheets within the same workbook, namely sheet1 ("rawdata") and sheet2 ("Overview).
I copy downloaded data into sheet1 ("rawdata"). Here the number of rows vary but heading/columns are always the same. After this I need to copy specific cells into another worksheet.
Here are the "rules" I was thinking about:
1) Always copy cells from the rawdata sheet E9, W9, X9 and Y9 into a specific cell in the target sheet. I had something like this (which worked):
Worksheets("overview").Range("X10").Value = Worksheets("rawdata").Range("E9").Value
2) Always copy the value within column E in the lastrow. However, the last row is varying from rawdata to rawdata while the column (E) stays the same. I tried something like this: (not working)
....= Worksheets("rawdata").Range("E1").End(xlDown).Value
3) The script should be linked to the button, when I click the button again to insert the data from the sheet rawdata, the data should be inserted in the next (following) column of worksheet overview.
Assumes column E always has data. Which in this case should be true.
Sorry tried to simplify and broke it.
LastRow_WithDataInColumnE = Worksheets("rawdata").Range("E" & .Rows.Count).End(xlUp).Row
Should be
With Worksheets("rawdata")
LastRow_WithDataInColumnE = .Range("E" & .Rows.Count).End(xlUp).Row
End With
Now .Rows.Count should refer to Worksheets("rawdata")
Worksheets("overview").Range("X10").Value = Worksheets("rawdata").Range("E" & .Rows.Count).End(xlUp).Row.Value
Should be
With Worksheets("rawdata")
Worksheets("overview").Range("X10").Value = .Range("E" & .Rows.Count).End(xlUp).Row.Value
End With
There is a discussion here Error in finding last used cell in VBA. Suggests a better solution for situations where there is no data in Column E or where rows have been deleted.
You could do something like this to get the last data range in column E:
Public Function FindLastColumnECellAvailable()
FindLastColumnECellAvailable = "E" & WorksheetFunction.CountA(Range("E:E"))
End Function
Then you will have this:
At the end just read the cell value:
Range(FindLastColumnECellAvailable).Value
Greetings
Sorry
An apologize "in advance", I just read the date, hope this will help you yet or anyone else, it's my second day