Move merged cell using VBA - 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!!!

Related

Excel visiual basic: increment trouble

I am working on a code that, upon each click it'll add a number to a list, that number is copied from one sheet and is pasted on another workbook. The problem i am running into is the increment coding. I've tried { activecell = activecell + 1 } but it is adding the numbers in a descending order i.e. 7,6,5,4 and etc.enter image description here
`Sub FILER()
workbooks.open("LOG")
Activeworkbooks.windows(1)visible=false
range("A3").activate
For Each Cell In Worksheets("LOG1").range("A3:A10")
If Cell.value > 0
ActiveCell.offset(1,0).select
ElseIf Cell.Value= 0 then
activeCell.offset(1) = ActiveCell + 1
End IF
Next
End Sub
You go through the cells within a range, no need to select cells explicitly. Avoid using .Activate and .Select. You only increment the cell values if the cell value is 0.
-> clean up and shorten that code and post it into your question.

Excel VBA: How to change value of cell in last used column?

I am new to VBA and am trying to locate the last used column (changes monthly) and change the second row cell in that column to "Apple." The values in the last used column start from row 5, but I need to change row 2. Can anyone help? This the code I have come up with, I understand it's flawed:
Sub NameCell()
.Cells(5, Columns.Count).End(xlToLeft).Column.Select
.FormulaR3C1 = "Apple"
End Sub
Sub NameCell()
With ActiveSheet
.Cells(2,.Cells(5, .Columns.Count).End(xlToLeft).Column).Value = "Apple"
End With
End Sub

VBA go to next row and pull formula from above after data has been entered

I have the excel formula below to pull data from another sheet.
=IF(ISNUMBER(SEARCH("Yes",Sheet7!C4)),Sheet7!A4,"")
These cells used, C4 and A4, will always remain the same. Different item numbers are being scanned in one after another so the values in the cells will change.
When the word Yes is shown in C4 i would like it to record the item # in A4 into the new sheet. This formula works great for that.
However, after the item # is recorded in the new sheet, i would like it to go down to the next row and copy the formula so it can record the next item # scanned.
Is this VBA possible? thank you!
I think you are trying to get something like
If Worksheets("Sheet7").Range("C4").Value Like "*Yes*" Then
ActiveCell.Value = Worksheets("Sheet7").Range("A4").Value
Else
ActiveCell.Value = ""
End If
but don't use ActiveCell - use a proper reference to the cell in which you want to put the value. (Your question doesn't contain enough information to determine what the location is, which is why I was forced to just refer to it as ActiveCell.)
Based on comments, it sounds like you want the following Worksheet_Change event in Worksheets("Sheet7"):
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count <> 1 Then
Exit Sub
End If
If Intersect(Range("A4"), Target) Is Nothing Then
Exit Sub
End If
If Range("C4").Value Like "*Yes*" Then
With Worksheets("Sheet1")
.Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Value = Target.Value
End With
End If
End Sub

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

VBA Go to last empty row

I have a project on excel macro, I need to highlight the next last row that has an empty value. example cell A1:A100 have data and the next cell is A101 is empty.
when user click a button it should highlight the cell A101...
If you are certain that you only need column A, then you can use an End function in VBA to get that result.
If all the cells A1:A100 are filled, then to select the next empty cell use:
Range("A1").End(xlDown).Offset(1, 0).Select
Here, End(xlDown) is the equivalent of selecting A1 and pressing Ctrl + Down Arrow.
If there are blank cells in A1:A100, then you need to start at the bottom and work your way up. You can do this by combining the use of Rows.Count and End(xlUp), like so:
Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Select
Going on even further, this can be generalized to selecting a range of cells, starting at a point of your choice (not just in column A). In the following code, assume you have values in cells C10:C100, with blank cells interspersed in between. You wish to select all the cells C10:C100, not knowing that the column ends at row 100, starting by manually selecting C10.
Range(Selection, Cells(Rows.Count, Selection.Column).End(xlUp)).Select
The above line is perhaps one of the more important lines to know as a VBA programmer, as it allows you to dynamically select ranges based on very few criteria, and not be bothered with blank cells in the middle.
try this:
Sub test()
With Application.WorksheetFunction
Cells(.CountA(Columns("A:A")) + 1, 1).Select
End With
End Sub
Hope this works for you.
This does it:
Do
c = c + 1
Loop While Cells(c, "A").Value <> ""
'prints the last empty row
Debug.Print c