excel vba select multiple columns in a loop - excel

Edit: I only have 28,000 columns, and you all are correct that they can't all fit in one worksheet. I was testing my code with only a portion of the data and hadn't yet realized that it will not all fit
I have 28,000 columns of data. I am trying to to copy specific columns 5,12,19,26...(ie for i=1:4000, column number = 7*(i-1) + 5). My original thought is below, but the problem is that after each iteration of the loop, the previous column is deselected. So the code below does not copy the intended data.
For i = 1 To 4000
DataSheet.Columns(7 * (i - 1) + 5).Select
Next i
Selection.Copy
ResultsSheet.Paste
I thought about the alternative below (which works, but very slowly), but I was hoping I could write something that executes more quickly (part of the problem is the code selects the destination sheet and pastes each column individually, essentially quadrupling the number of steps as something similar to the first solution).
For i = 1 To nSymbols
DataSheet.Columns(7 * (i - 1) + 5).Copy
ResultsSheet.Select
Columns(i+1).Select
ActiveSheet.Paste
Next i
Any ideas on how to make this code run (faster)?

Use Union and increment your For ... Next by 7 for each increment.
dim c as long, rng as range
with worksheets("sheet1")
set rng = intersect(.columns(5), .usedrange)
for c = 12 to 4000 step 7
set rng = UNION(rng, intersect(.columns(c), .usedrange))
next c
end with
debug.print rng.address(0, 0)
rng.copy destination:=ResultsSheet.cells(2, 1)
I've added Intersect with UsedRange to reduce the full column references. Due to the Union'ed range, this Copy & Paste resjults in a Copy, Paste Special, Values and fORMATS.

Related

How to add serial number in "different size merged cells" which are positioned "alternatively" in excel using VBA?

As shown in image, I am trying to fill numbers in increasing order in alternate merged cells, which are different in size. So, I can't use autofill function of excel. But I want a macro so I can do it every time just hitting button once.
Note that I want numbers till the used range only.
I tried a lot to do it my self, but I am stuck now...Plz help the beginner, it's my third day in VBA.
This should do what you are looking for.
It will ignore non merged cells, I didn't see any in your screenshot that needed a number and were not merged so that shouldn't be an issue.
It uses column B to figure out the last row of your data.
Dim i As Long
Dim lr As Long
Dim counter As Long
counter = 1
With Sheet1 'Change to whatever your sheets code name is
lr = .Cells(.Rows.Count, 2).End(xlUp).Row 'If you want to use something other than column B, change the 2 to the right column index
For i = 2 To lr
If .Cells(i, 1).MergeCells = True Then
If .Cells(i, 1).MergeArea.Item(1).Address = .Cells(i, 1).Address Then
.Cells(i, 1) = counter
counter = counter + 1
End If
End If
Next i
End With

Looping imports through different sheets

I am a newbie to VBA but got tasked to work with it anyway. So my task is to build a macro that takes data from different sheets and puts it below each other in one result sheet ("Tabelle1" in my example). The input data in each sheet is stored in blocks of two columns, right next to each other - so columns A and B have to import into the result sheet, then C and D and so on. Doing this for one sheet is not a problem:
Sub Makro1()
'
' Makro1 Makro
'
Dim Erste As Long
Dim k As Long
Dim j As Long
k = 1
j = 2
Do
Sheets("Tabelle1").Select
Erste = ActiveSheet.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).Row
Sheets("Tabelle2").Select
Range(Cells(5, k), Cells(5, j)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Tabelle1").Select
Cells(Erste, 3).Select
ActiveSheet.Paste
k = k + 2
j = j + 2
Loop Until Sheets("Tabelle2").Cells(4, k).Value = ""
End Sub
But I not only have one input sheet ("Tabelle2" in this example) but several (up until sheet20 or so). And all of them are built the exact same way, only with different data in each. What I would need the macro to do is, when reaching the empty cell in the first input sheet ("Tabelle2), go to the next input sheet ("Tabelle3") and continue the import of the data.
It doesn't sound too hard to do at first, but I cannot seem to find a solution. If anyone could help me out, it would be very much appreciated :-)
I know that the macro itself is very badly written and I can get rid of most of the Select. But as long as it works I'm fine.
Instead of using the name of the sheet, use the Index
Example: Sheet(1) and Sheet(2) etc
You can use that number as a variable that you can increment.
Example:
Dim i as Integer
Sheet(i).Select
Note:
It is also better practice to change the code to not rely on .Select as it can cause confusion and problems.
In addition, it would be better to use Worksheet(1) as charts can also be referred to as sheets.

Excel Macro to copy x numbers of rows separately

I would like to create a excel Macro that allows to me copy an x number of rows of certain column. For Example, I have column K that I have in 10.500 rows. I want to copy 1000 lines each time and also the 500 lines at the end. any help with the coding part ? I looked on so many sites and no success. I don't need to paste the copied number in any other excel sheet. I just need the macro command to copy me 1000 lines every time from the column that I selected.
thank youuuuu very much and much appreciated !
Cheers
Select a column and run the sub procedure from the Macros dialog (Alt+F8). The first 1000 cells will be copied to the clipboard.
Paste the data into another program.
Return to Excel and run the sub procedure again. The next 1000 rows of data will be copied to the clipboard.
When the last group of data has been copied to the clipboard, a message box will notify.
Option Explicit
Sub progressiveCopy()
Dim m As Long
Static i As Long, k As Long
Application.CutCopyMode = False
m = 1000
If k <> ActiveCell.Column Then
k = ActiveCell.Column
i = 0
End If
With Worksheets("sheet1")
m = Application.Min(m, .Cells(.Rows.Count, k).End(xlUp).Row - i)
.Cells(i + 1, k).Resize(m, 1).Copy
i = i + m
If i >= .Cells(.Rows.Count, k).End(xlUp).Row Then
MsgBox "This column is complete. Select another column next time."
End If
End With
End Sub
You may wish to set up a hot-key combination for the sub procedure in the Macros dialog to ease repetitive operations.
This code finds the number of rows in the currently selected column. Checks if the number to be copied is less than the rows left. Selects the range and puts the range in copy mode. After the range is copied you would have to go to they sheet, document, or whatever to paste the data.
I've modified the code to then select down 1000 rows or what ever is left in the column. So you should be able to run the code, paste the data, run the code, paste the data. When you hit the end, a message tells you that you are at the end of the column.
Application.CutCopyMode = False
numRowstoCopy = 1000
varCurrentRow = ActiveCell.Row
varCurrentColumn = ActiveCell.Column
FinalRow = Cells(Rows.Count, varCurrentColumn).End(xlUp).Row
varRowsToEnd = FinalRow - varCurrentRow
If varRowsToEnd < numRowstoCopy Then
Range(Cells(varCurrentRow, varCurrentColumn), Cells(varCurrentRow + varRowsToEnd, varCurrentColumn)).Select
Selection.Copy
MsgBox "Last Rows to Paste Have been copied"
Else
Range(Cells(varCurrentRow, varCurrentColumn), Cells(varCurrentRow + numRowstoCopy - 1, varCurrentColumn)).Select
Selection.Copy
ActiveCell.Offset(numRowstoCopy, 0).Select
End If

Selecting a range using Cells function in vba

I want to select I15:R15 and I20:R20 using Cells() function in vba. Can't use Range as the row number will always be different.
In first go, I want to copy and pasteI15:R15 and I16:R16 into a sheet.
In next go, I want to copy and paste I15:R15 and I17:R17 , In next go, I want to copy and paste I15:R15 and I18:R18 ..... and so on.
If I use Range() function, then I don't know whether it will always be Copying I15:R15 or I15:S15 or so on, basically I15 is fixed, the right side of range (i.e., column whether R or S or T is not decided.
After reading your updated post, you are looking for something like the code below:
Dim Rng As Range
Dim i As Long
For i = 16 To 20
' Set a dynamic range of "I15:R15" and another row, starting from row 16 and incrementing untill 20
Set Rng = Application.Union(Range(Cells(15, "I"), Cells(15, "R")), Range(Cells(i, "I"), Cells(i, "R")))
Rng.Copy
' do your Paste code here
Set Rng = Nothing
Next i

Excel Compare two cells in one sheet to two in another, copy a certain range of cells if they are equal

Sorry if this is a stupid question(I searched and couldn't find a answer.) I was trying to find out if this was possible with functions but it seems like I will need to use a macro, I don't have any experience with them but can learn.
I am trying to compare 2 cells in sheet 1, Resource name and project code(C and L) against two of the same named columns in sheet 2(where they are A and D). The Resource name is formatted like: Lanier, Joe so its last name comma space first name. Project code is a mix of letters and numbers with no spaces. If they are the same, I would like to copy a range of cells in sheet 1(T through Y) into the matching row's columns X through AC in sheet 2. It would overwrite any data in those cells.
If it is also possible, if there was a way to highlight the cells or rows that don't have a match that would be such a great help so my boss would know what he needed to manually copy over. Thanks so much!
EDIT: Included below macro that seems like it should work but isn't. It is highlighting all of the cells. Any idea what could be changed?
Sub ertert()
Dim i&, j&, s$, col As New Collection
Application.ScreenUpdating = False
On Error Resume Next
With Sheets("Sheet2")
For i = 1 To .Cells(Rows.Count, 3).End(xlUp).Row
s = .Cells(i, 1) & "~" & .Cells(i, 3)
If IsEmpty(.Item(s)) Then col.Add i, s
Next i
End With
With Sheets("Sheet1")
.Columns(1).Interior.Color = xlNone
For i = 1 To .Cells(Rows.Count, 1).End(xlUp).Row
s = .Cells(i, 3) & "~" & .Cells(i, 12)
If IsEmpty(col.Item(s)) Then
.Cells(i, 1).Interior.Color = vbYellow
Else
j = col.Item(s)
Sheets("Sheet2").Cells(j, 5).Resize(, 6).Value = .Cells(i, 24).Resize(, 6).Value
End If
Next i
End With: Application.ScreenUpdating = True
End Sub
edited July 23 - 10:30EST
no VBA or macro needed you can use simple formulas and some copy and paste
on sheet2, at the end of the data columns, create a new column (AD?) with this formula
=IF(AND(IFERROR(VLOOKUP(A4,[book1]sheet1!$C$4:$C$14,1,FALSE),"")<>"",IFERROR(VLOOKUP(D4,[book1]sheet1!$L$4:$L$14,1,FALSE),"")<>""),[book1]sheet1!T4,X4)
and copy it over the next 6 rows (AD to AI)
I assumed the first row being 4 and the formula is searching 10 rows in sheet 1, adjust as needed then copy down to last of your data rows
if you have a match, the sheet1 data will be displayed otherwise, sheet2 data will be there
copy this chunk, go to X4 and use EDIT - PASTE SPECIAL - VALUES if excel 2003 or click the PASTE button on upper left and choose PASTE VALUES if Excel 2007+
I recommend you get information on VLOOKUP to better understand this solution
cheers

Resources