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

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

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!!!

find text in column in one sheet and copy row data to another sheet

I have a spreadsheet with customer information that I want to search on by last name. I want to enter the last name on a separate sheet (Sheet 1) and have the macro search the Last Name column in the customer data spreadsheet (Sheet 2). When it finds a match, I want it to copy the entire row in Sheet 2 and paste it to a specific row in Sheet 1. I've searched a number of sites and tried numerous versions of code but cannot get it to work.
Here's a link that shows you how to get data from another sheet or workbook. Basically you use Sheet_name!Cell_address or Sheet_name!First_cell:Last_cell.
Hope this helps :)
I think this sounds simple enough, loop until you find the value you want. How do you want the trigger to fire? the below into sheet 2 into the will trigger after double clicking on a selected cell in column 1, will prompt you for input, then copy the first match.
Option Explicit
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Column <> 1 Then Exit Sub 'or which ever column you enter for
Dim str_Act, str_Test As String
Dim i As Integer
'find value to search
str_Act = InputBox("Enter User Last Name")
If str_Act = "" Then Exit Sub
'loop to find search
Do While str_Act <> str_Test
str_Test = Sheets(1).Range("A1").Offset(i, 0) ' or whichever column has your value
i = i + 1
Loop
'Copy and paste
Sheets(1).Range("A1:ZZ1").Offset(i - 1, 0).Copy
Target.PasteSpecial
End Sub

Remove certain columns from sheet

I have a master sheet, but out of this sheet I only need certain columns to be displayed. Doing it manually is taking a long time and this worksheet is something I have to do once in a fortnight.
Can anyone please suggest VBA code to do this?
Let's say you need to delete columns numbers 13, 58 and 101, then do as follows:
Sub DeleteColumns()
Columns(101).EntireColumn.Delete
Columns(58).EntireColumn.Delete
Columns(13).EntireColumn.Delete
End Sub
For keeping the original column numbers, make sure you delete them from the highest to the lowest. Note that columns are numbered from 1 (which is "A") onwards.
If you don't want to delete the columns, but just hide them, then use the Hidden property:
Sub HideColumns()
Columns(13).EntireColumn.Hidden = True
' ... etc.
End Sub
You could go as follows:
Sub ColumnsDelete()
Range("A1, E1, AH1").EntireColumn.Delete
End Sub
Sub ColumnsHide()
Range("A1, E1, AH1").EntireColumn.Hidden=True
End Sub
Where you simply have to type columns headers followed by any row number (I chose "1" for simplicity)

Excel Drop Down Box with Formula

Hi I have an excel with a drop down box whose list has 3 cells. One of these cells contain a formula. The problem is this formula is dependent on data in another cell and when this data changes the calculated value changes. The value is automatically update in the list where it was chosen from but I will manually have to go back to the drop down box and change it. How can I have the value be updated automatically. Willing to look at a VBA solution if need be
Put the following into the worksheet module. It assumes that the cell with validation applied is G9, and the second option of the list is the formula.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False) = "G9" Then
If Target.HasFormula Then Exit Sub 'or else infinite loop
Dim ListRange As Range
Dim FoundIdx As Variant
Set ListRange = Me.Evaluate(Me.Range("G9").Validation.Formula1)
FoundIdx = Application.Match(Target.Value, ListRange, 0)
If Not IsError(FoundIdx) Then
If FoundIdx = 2 Then
Target.Formula = ListRange(2).Formula
End If
End If
End If
End Sub
Note that this will not work if the formula might have the same value as any of the other options!
I couldn't reproduce your issue. Here is what I did:
Populate 2 cells with a random value and a third cell with a formula (columns B3, B4, B5)
Define a Name Range with these 3 cells called it Options
Create a drop down using Insert/Form Controls/Combo Box
Set the input range of the drop down to Options
Change the cells value to get the formula give different results and the new values are reflected on the Options list and the Drop Down.
Is this what you are doing?

sumif code returns error when using column number?

I have a quick question... not sure what I am doing wrong.
I would like to have a named range (single cell) updated with the value from a sumif based on data in another tab of excel. the formula should go through column 2 look for the date and sum any values in column 10.
even when I substitute out the columns with actual hard column letters, I am getting error 1004 method range of object worksheet failed. how can I re-code this to pick up sumif data from another tab?
here is my code
with data_ws
date = #5/13/2014#
[named_range] = worksheetfunction.sumif(.range(.columns(2)), date, _
.range(.columns(10))
end with
You are close and may have an idea from below why yours are not working:
Sub TestSumIf()
Dim oRngA As Range, oRngB As Range
With ActiveCell
.ClearContents
Set oRngA = .Columns(2).EntireColumn ' 1st EntireColumn on the right
Set oRngB = .Columns(3).EntireColumn ' 2nd EntireColumn on the right
.Value = WorksheetFunction.SumIf(oRngA, Date, oRngB)
End With
End Sub
Sample: