How to change values in spesific cells after copying VBA - vba

I need to change all the values in a workbook that is not active. All the values are listed in the same column.
In the code i'm first going to copy some specific cells, after copy them I want to set the value = 0 in cells C3:C19. My code looks like this:
Range("A3:D19").Copy
ActiveWorkbook.Close False
I then tried to used replace on C3:C19, this did not work.
Thanks :)

Worksheets(1).Range("A3:D19").Copy
Worksheets(2).Range("A3:D19").Paste ' <~~ Do whatever else here
Worksheets(1).Range("C3:C19").Value = 0

It will be better if you could show some of your code.
Range("whatever") = 0

Related

Move merged cell using VBA

I'm very new at VBA, so I find myself in a bit of a hazzle.
I'm trying to move a merged cell up dependent on a specific value in another cell.
Cell D4 contains a value between 1 and 4, and it is dependent on a formula.
When this value is equal to 1 I'd like for the merged cell BQ52:BX64 to move up to row 40, and not replace the cells, but shift them downwards.
When the value is between 2 and 4 I'd like for the cells to shift back to their original location.
I've tried to record macros of me inserting copied cells, but I'm unsure as to how to code this in VBA and how to avoid a loop, since I'm deleting the cells in the recording.
The name of the sheet is "Print Layout"
Any help is much appreciated!
Sub random()
If Range("D4").Value = 1 Then
Range("BQ52:BX64").Cut
Range("BQ40").Select
Selection.Insert Shift:=xlDown
End If
End Sub
This will put the merged cell at row 40 if d4 = 1 otherwise the merged cell will remain there.
If you can name your sheet in VBA something like Print_Layout in the properties window this may help avoid issues in the future. You could then use code such as:
Sub MoveMergedCells()
Print_Layout.Select
If Range("D4") = "1" Then
Range("BQ52:BX64").Cut
Range("BQ40").Insert Shift:=xlDown
End If
End Sub
You could also add an If/Then function for values 2-4. Hope this helps :)
As per the answer given by #L Johnstone, complete code can be given as:
Sub MoveMergedCells()
Print_Layout.Select
If Range("D4") = "1" Then
Range("BQ52:BX64").Cut
Range("BQ40").Insert Shift:=xlDown
Else If Range("D4") = "2" Then
Range("BQ40:BX52").Cut
Range("BQ52").Insert Shift:-xlDown
Else If Range("D4") = "3" Then
Range("BQ40:BX52").Cut
Range("BQ52").Insert Shift:-xlDown
Else If Range("D4") = "4" Then
Range("BQ40:BX52").Cut
Range("BQ52").Insert Shift:-xlDown
End If
End Sub
I have tried my best to answer this, apologies if anything misses.
Thanks!!!

Copy rows based on cell value and paste on a new sheet

Check This
I need a help. I want to copy whole cell from A sheet name "Components" only if value in Column C is > 0 to a new Sheet name "Load list"
Can someone please give me the macro code for this?
on your new sheet you can add this condition the cell or range of cells:
=IF(Components!C5>0,Components!A5)
where C5 has thevalue to compare, and A5 has the value copy if the condition happens.
Right in my swing!
The formula given by #sweetkaos will work fine, in case you want to replicate the data as it is with blanks where data is not found.
I will imagine a slightly more complicated situation. I am assuming you want just one line in the next format as is shown in your image.
Also conveniently assuming the following:
a. both sheets have fixed start points for the lists
b. 2 column lists - to be copied and pasted, with second column having value
c. Continuous, without break source list
d. basic knowledge of vba, so you can restructure the code
Here is the code. Do try to understand it line by line. Happy Excelling!
Sub populateLoadList()
'declaring range type variables
Dim rngStartFirstList As Range, rngStartLoadList As Range
'setting values to the range variables
'you must change the names of the sheets and A1 to the correct starts of your two lists
Set rngStartFirstList = Worksheets("Name_of_your_fist_sheet").Range("A1")
Set rngStartLoadList = Worksheets("Name_of_your_second_sheet").Range("A1")
Do While rngStartFirstList.Value <> ""
If rngStartFirstList.Offset(1, 0).Value < 0 Then
Range(rngStartFirstList, rngStartFirstList.Offset(0, 1)).Copy
rngStartLoadList.PasteSpecial xlPasteValues
Application.CutCopyMode = False
Set rngStartLoadList = rngStartLoadList.Offset(1, 0)
End If
Set rngStartFirstList = rngStartFirstList.Offset(1, 0)
Loop
End Sub
Basically what i want is ... if Value on C is >0 i want whole column 10 copied to that new sheet .... not only that cell

I want to hide a column in excel using VBA macro in run time but while executing other columns also get selected.

I want to hide column C based on value in cell AB7.
If value in cell AB7 is 117 then entire column C needs to hide.
Else I want column C to be there.
If Range("AB7").Value = "117" Then
Columns("C:C").Select
Selection.EntireColumn.Hidden = True
Else
Columns("C:C").EntireColumn.Hidden = False
End If
When I run the code columns A:G also get selected. Please let me know if there are any limitations while using the above code. I use excel 2010. Thanks in advance.
I would recommend not using select. Columns("C:C").Hidden = False and Columns("C:C").Hidden = True

Excel VBA, Checking for value from a cell in the same row

I have a macro that needs to check if there is a 1 in a cell from the same row. The problem I have is that I need to do that for a range of 3 columns.
Actually, this part doesn't work at all but I can put it in cells form.
I need to check for all the cells from ("P3:R" & iNbKids) if there is a 1 in the cell ("K" & ActiveCell.Row). I have tried a lot of things but I can't seem to figure it out.
iNbKids is an Integer
Hard to say for sure, but it looks like you need to change
If Cell.Value <> 0 Then
To
If Range("K"&r2.row).Value <> 0 Then
Or
If Range("K"&r2.row).Value = 1 Then

Write multiple cells at once in Excel VBA

In Excel VBA I am using some code to update the cells. Like:
for i = 1 to 1000
for j = 1 to 1000
cells(j,i)=<whatever_different_in_each_cell>
next
next
Is there a way to update all cells at once, instead of updating each cell individually ? Reason is that updating one cell in Excel is time consuming and when there are many of them, then it quickly becomes endless.
Range(<...>).Value = Array (value1, value2, <...>, valueN)
The number of values must be exactly the same as the number of cells in the Range, otherwise, the range will be filled with #VALUE!s.
range(cells(1,1),cells(1000,1000))="whatever"
Try this:
Sub WriteMultipleCells()
x = "whatever"
ThisWorkbook.Worksheets("Sheet1").Range("A1:X1") = x
End Sub