Hello attached is an excel sheet with a macro to copy paste columns having specific heading. will someone help me concise the macro. Right now its a huge page of codes. I am new to vba and learning by recording and then modifying.
Hello attached is an excel sheet with a macro to copy paste columns having specific heading. will someone help me concise the macro. Right now its a huge page of codes. I am new to vba and learning by recording and then modifying.
Hi, krash297,
insetad of switching sheets set object to the sheets. And instead of having multiple codes for each item use an array which holds the values and loop through that array:
Ciao,![]()
Sub EF938993() Dim arrFinds Dim lngArray As Long Dim wsSign As Worksheet Dim wsSgnot As Worksheet Dim rngFound As Range Dim lngCol As Long Dim lngLast As Long arrFinds = Array("Project", "Activity", "Activity and Work Area", "WA Stage", "Status", _ "QIL Error Rate", "QQ Error Rate", "Start Date WA", "End Date WA") Set wsSign = Sheets("Signout") Set wsSgnot = Sheets("Sgnotproces") For lngArray = LBound(arrFinds) To UBound(arrFinds) Set rngFound = wsSign.Cells.Find(arrFinds(lngArray), , , , xlByRows, xlNext, True, , False) If Not rngFound Is Nothing Then lngLast = wsSign.Cells(Rows.Count, rngFound.Column).End(xlUp).Row lngCol = lngCol + 1 wsSgnot.Range(wsSgnot.Cells(1, lngCol), wsSgnot.Cells(lngLast, lngCol)).Value = _ wsSign.Range(wsSign.Cells(1, rngFound.Column), wsSign.Cells(lngLast, rngFound.Column)).Value End If Next lngArray wsSign.UsedRange.ClearContents Set wsSgnot = Nothing Set wsSign = Nothing End Sub
Holger
Use Code-Tags for showing your code: [code] Your Code here [/code]
Please mark your question Solved if there has been offered a solution that works fine for you
i am new to vba i dont know how to use arrays could you explain me how this works so that i can relate it.
Hi, krash297,
the array stores the names of the items you want to search. In the code it is filled with the line
Any item should be packed in quotes (it it is a string) and be separated by comma. This type of array is zero based which means that the first item will have the number of 0.![]()
arrFinds = Array("Project", "Activity", "Activity and Work Area", "WA Stage", "Status", _ "QIL Error Rate", "QQ Error Rate", "Start Date WA", "End Date WA")
Next we do have a loop which will go from the lower bound of the array (as I just stated this will be 0) to the number of items being stored - 1. Instead of writing numbers we use the command LBound (lower bound) and UBound (upper bound) which will free us to alter the code if the number of the items to search for is changed.
Ciao,
Holger
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks