loop and select next cell excel macro - excel

Hello hope all is well :)
having trouble figuring out how to loop and select next cell
so when a cell in the range h3:z3 is empty it would stop :)
and what it is doing is selecting the value from h3 pasteing in b3 runs a another macro which gives an order number in e3 which is then copied and pasted in h4 then it would go to the next cell in I3 paste in b3 copy result from e3 and paste in I4 and do the same
thanks
For Each cell In Range("H3:Z3")
If IsEmpty(cell.Value) Then Exit For
'select ammount and place in lookup
Range("H3").Select
Selection.Copy
Range("B3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
' fill in order numbers
bulkON_Click
'select order and past under postcode
Range("E3").Select
Application.CutCopyMode = False
Application.CutCopyMode = False
Selection.Copy
Range("H4").Select
ActiveSheet.Paste
Loop

There's a lot that I would probably change in this code. This should get you started.
For starters, a For loop requires a Next statement, not a Loop (which is used for Do blocks. Also, you should avoid copying/pasting at all costs, in favor of writing values directly to the destination cell(s).
I assume that cells "B3" and "E3" are constant, and you are iterating over cells in H3:Z3 and computing some value to put in corresponding cell in H4:Z4.
For Each Cell In Range("H3:Z3")
If Cell.Value = vbNullString Then Exit For
'select ammount and place in lookup
Range("B3").Value = Cell.Value '<< no need to "copy & paste", just write the value directly
' fill in order numbers
bulkON_Click
'insert the value under postcode
' this OFFSET refers to the cell 1 row below the "Cell"
Cell.Offset(1, 0).Value = Range("E3").Value
Next

Related

Is there a way to format exports from azure differently in excel?

I just started working with large quantities of data from Azure and other software products that produce CSVs or excel files that produce information formatted as follows:
Is there an easy way to format it like:
So that it can be used effectively as a table?
Thank you!
Here's an algorithmic approach that you can implement in VBA
Get references to the Sheet and Range containig the data
Copy the Data to a Variant Array variable
Loop throught the Data Array rows
For each row that column A is Not Empty, capture the value
For each row where Column B is Empty, clear Column A
For each row where Column B is Not Empty, write the captured value to Column A
After the loop copy the Data Array back to the sheet
This works for me.
Sub TryMe()
' fill down from above
Range("A1:A20").Select ' change the range to suit your needs
Range("A20").Activate
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.FormulaR1C1 = "=R[-1]C"
' copy/paste values; no references to other cells
Cells.Select
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("A1").Select
' use come logic to do cleanup
Dim rng As Range, cell As Range
Set rng = Range("B1:B20") ' change the range to suit your needs
For Each cell In rng
If cell.Value = "" Then
cell.Offset(0, -1).Value = ""
End If
Next cell
End Sub
Before:
After:

IF function and Cycle

I have a question regarding excel vba and the use of the "IF" function with the "For" cycle. I will try to be as clear as possible.
here is the code:
sub cicleFor()
For i= 1 to 2
Calculate
Range("E3").Select
Selection.Copy Sheets("Sheet2").Select Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet1").Select
Range("C14").Select
Next
It is pretty simple! I take E3, copy paste into another cell (A2) for two times.
I use calculate at the start because the number in E3 will change each time
What I would like to have is the following:
to use an "IF" function that, if A2 is full, goes to A3 and so on, for i = 1 to 100.
since I have used a for function, i want A2,A3,A4,...A100 to be filled with the result of E3 of the sheet1.
I am not an expert as you can see!
if you have any hint, I will be grateful!
Thank you!
EDIT to add the case of multiple cells
maybe you're after this
Sub cicleFor()
For i = 1 To 100
Calculate
Sheets("Sheet2").Cells(1 + i, 1).Value = Sheets("Sheet1").Range("E3").Value
Next
End Sub
if you need to copy/paste the content of more than one cell then you want to use Resize(nRows, nColumns) property of Range object to properly adjust the "target" range size to fit the "source" one
for instance to copy/paste the content of range "E3:H3" (i.e. four columns) you want to use:
Sub cicleFor()
For i = 1 To 100
Calculate
Sheets("Sheet2").Cells(1 + i, 1).Resize(,4).Value = Sheets("Sheet1").Range("E3:H3").Value
Next
End Sub
Your code could be much shorter - no need for select/copy/paste if you only want to transfer the values. Use End(xlUp) to locate the last used cell.
Sub cicleFor()
For i= 1 to 2
Calculate
Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Value = _
Sheets("Sheet1").Range("E3").Value
Next
End Sub

VBA copying cell dependent on text to next blank cell on another worksheet

I created the below and i'm an absolute noob, i literally just pieced different bits of info together to get it working and it did, until i added the selecting the cell if it has 'r' in it and moving it to 'sheet7' and now i get an (object required) error when it runs.
I really need some help on this and if you are feeling generous, i would like to repeat the exercise with several other letters and sheets, so if you could demonstrate an additional one too, i'm sure i could work out the rest.
Thanks in advance
Sub Macro1()
'
' Macro1 Macro
'
'
Range("I16").Select
Selection.Copy
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Offset(0, 1).Select
Application.CutCopyMode = False
Selection.Copy
Range("J20").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveWindow.SmallScroll Down:=-27
Range("I16").Select
If Cell.Value = "R" Then
Range("J20").Select
Selection.Copy
Sheets("Sheet7").Select
Range("B" & Rows.Count).End(xlUp).Offset(1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
End Sub
Generally when writing macros it's good practice to avoid selecting cells. Selecting a cell is really something that only a human does, but in VBA we can just refer to the cell directly without touching it.
Rewriting your macro to avoid all the touching:
Sub Macro1()
'Make a variable to store the cell found
Dim lastCell as Range
'find the last cell in Column A of the active sheet
lastCell = Range("A" & Rows.Count).End(xlUp).Offset(1)
'Paste in the I16 value
lastCell.value = RangE("I16").value
'Grab whatever is hanging out in Column B next to the last cell and stick it in J20
Range("J20").value = lastCell.Offset(0,1).value
'Test to see if I16 has value "R"
If Range("I16").value = "R" Then
'Find the last row in Sheet7, Column B and store it to the variable
lastCell = Range("B" & Rows.Count).End(xlUp).Offset(1)
'Copy J20 value to the lastCell in Sheet 7, Column B
lastCell = Range("J20").value
End if
End Sub
I'm not sure where you were getting the error you reported. It's probably specific to your workbook, so we'd have to be sitting in front of it to track it down. This rewrite may correct it though.
Also, it's not clear what else is happening in this process. My guess is that Column B of the Active Sheet has a formula in it that does something to the value we paste from I16. After it's pasted and calculated we grab that and stick it in J20 and if I16 is equal to "R" then we put that calculated J20 value over Sheet7. If that sounds about right, then the macro above should do the trick.
Also, if that IS what's happening, then perhaps you could share the formula you have in Column B. We can probable to do that calculation within VBA and save a ton of steps in this macro.

Vba to reference a cell but remain static in the new cell

My workbook has data that is automatically loaded in from yahoo finance. The data is constantly being refreshed. I need a formula that can take the referenced cell and produce the number value of that cell without updating whenever the referenced cell does. For example: Cell G3 will say 40.75 for the stock value and I need D2 to say 40.75 except that it will remain 40.75 when G3 updates to the new price.
I have tried using =Numbervalue($G$3) but that still updates when the data refreshes.
UPDATE
This is the VBA Code that I have to paste the Date and Time in the active cell (Which is correct) then it will paste the price (G3) in the cell of D2.
Sub TimeStamp()
'
' TimeStamp Macro
'
' Keyboard Shortcut: Ctrl+Shift+T
'
ActiveCell.Formula = "=CONCATENATE(L1,N1)"
ActiveCell.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("D2").Value = Range("G3").Value
End Sub
My problem is that I need the Range("D2").Value = Range("G3").Value part of the code to recognize the next empty cell in Column D. Currently, it will replace whatever data is already in the D2 cell.
You can write on your macro to the first empty row on column D by replacing this line:
Range("D2").Value = Range("G3").Value
With this one:
ActiveSheet.Columns(4).End(xlDown).Offset(1, 0).Value2 = Range("G3").Value2
Regards,

Excel Macro to copy from sequential rows to constant location, then from constant location back to matching row

I'm making my first macro in order to save having to perform 2500 copy-pastes. I have a long and complicated worksheet that takes two variables as inputs and returns a single value, and another sheet with 2500 pairs of these variables.
To keep things in the same sheet, I've linked the formula sheet inputs to J2 and K2 on my variable sheet, and the output to L2. My goal is to populate a third column next to the first two with the results for that row, by copying the two values to J2 & K2, then copying from L2 to the appropriate cell in the third column. As my macro is currently, it returns to the same cell in the third column every time, based on an offset from L2 as the last active cell.
I've tried searching for help on how to either increment the last paste operation, or to keep the active cell referencing the start point of the macro in order to keep things in the same row, but was unsuccessful. Any help would be appreciated.
ActiveCell.Range("A1:B1").Select
Selection.copy
Range("J2:K2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("L2").Select
Application.CutCopyMode = False
Selection.copy
ActiveCell.Offset(43, -5).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sub TT()
Dim sht As Worksheet, c As Range
Set sht = ActiveSheet
For Each c In sht.Range("A1:A2500").Cells
sht.Range("J2").Value = c.Value
sht.Range("K2").Value = c.Offset(0, 1).Value
sht.Calculate
c.Offset(0, 2).Value = sht.Range("L2").Value
Next c
End Sub

Resources