Expand a copy Range - excel

I have a button driven macro that copies a range of cells to the clipboard (so I can place in other documents):
Sub Button2_Click()
'
' Button2_Click Macro
Range("A1:p43").Copy
End Sub
Form time to time I need to either add or delete a row in that range in the worksheet. What I need is for the copy range to expand (or contract) to the number of rows containing the values. Note: the columns will not adjust, just the rows.

If the copy range is a contiguous range of values with a blank column in column Q and a blank row below row 43, you can use the CurrentRegion property like this:
Range("A1").CurrentRegion.Copy
It's basically the same as clicking A1 and then using Ctrl-Shift-Down arrow and Ctrl-Shift-Right arrow.

Related

Autofit a row with respect to a cell but not all the rows of the cells

I have a table in Excel (listobject) which loads texts in the cells depending on certain criteria.
Once loaded I would like to "autofit" the cells of a column (all cells of the listobjects have property wraptext=true) But I do not want autofit of the cells of column 5 (because they contain many lines each.
This is the code I have so far
dim SRTbl as listobject
set SRTbl = thisworkbook.sheets(1).listobjects(1)
' all rows height 14
SRTbl.DataBodyRange.RowHeight = 14
'I would like that the cells of column 4 are autofit-ed
'but NOT the cells of column 5
SRTbl.listcolumns(4).databodyrange.entirerow.autofit
This does not work because it autofits the whole row. In Column 5 the cells contain a multy-line text and I only want the autofit for the content of the cells of column 4
The following does not work neither:
SRTbl.ListColumns(4).Cells.autofit
Just in case it is not entirely clear.
Column 4 contains text having 1, 2 or 3 lines.
Column 5 contains text having many lines >10
I want to adjust the height of the cells to the lines of the cells of column4 but not column 5.
thanks
Put a break point on the line:
SRTbl.listcolumns(4).databodyrange.entirerow.autofit
. In the Immediate pane of the IDE enter:
SRTbl.listcolumns(4).databodyrange.entirerow.select and you will see the whole row selected.
Change SRTbl.listcolumns(4).databodyrange.entirerow.autofit to SRTbl.listcolumns(4).autofit and it should work.
If we have a look at what your initial code was doing it was attempting to autofit every cell in the EntireRow of the column you selected. You just need to change the reference from .EntireRow to .EntireColumn.
The confusing thing here appears to be in the syntax as you might expect to be able to call Autofit directly from the table or range objects. Autofit is a function of the .EntireColumn object which in turn is a subset of either the .Range or .DataBodyRange objects.
Option Explicit
Sub test()
Dim SRTbl As ListObject
Set SRTbl = ThisWorkbook.Sheets(1).ListObjects(1)
'I would like that the cells of column 4 are autofit-ed
SRTbl.ListColumns(4).Range.EntireColumn.AutoFit
End Sub

paste data into active cells

I write a very simple code
Sub copy_unibeton_muss()
Worksheets("source").Range("a8:c8").Copy
End Sub
The code will copy the range A8:C8. I added another portion to paste active cells in the active selected range.
Destination:=ActiveCell
But unfortunately the code paste only the data in the first row onlly. I need to paste data in all selected range may be 5x3 or 6x3 whatever I needed.
Use
Worksheets("source").Range("a8:c8").CopY Destination:= Selection
This will work as expected provided Selection has same number of columns of copied range

VBA replace value with cell above until blank and loop

I'm looking to fill values down based on the cell above until blank cell, this should continue until there is no more content, for example: refer the below table, copy the top value down until blank, therefore, 'petty msa' will copy down to 'inter' and stops, 'petty ksm' will copy down until 'general', 'comp' will copy down until 'motor'.
Column: A
petty msa
inter
petty ksm
welfare
water
prepay
general
comp
travel
motor
Edit: This is a long list of approx 15,000 rows
Thanks
Select the first cell where you have value 'petty msa' then-
1. press ctrl+shift+down -to select the range to be copied
2. press ctrd+down - to copy down the values
Now you have copied 'petty msa' upto the cell containing 'inter'
repeat this process for each range.
given your data "structure" as per your example, this should do:
Option Explicit
Sub main()
Dim area As Range
With Worksheets("filldown").Columns("A").SpecialCells(xlCellTypeConstants) '<--| change "filldown" to your actual worksheet name and "A" to actual index of column with data to be "filled down"
For Each area In .Areas
area.Value = area(1)
Next area
End With
End Sub
Insert a blank row in A1. In cell B2 paste the below formula and copy it down.
= if(A1="",A2,B1)
This should work

Highlighting rows where a cell contains a name from a list of names

How would I go about highlighting rows which contain a cell that contains a name from a list of names which I can specify?
I assume this is best done by a macro, but not sure where to start.
Place this code in a module
Option Explicit
Public Sub ApplyConditionalFormattingsFromAList()
'
' this code create multiple conditional formattings on current selected cells
' using a list of conditions along with its formattings defined in another worksheet.
' to use, just select the range and then run this code
'
Dim iRng As Range
Dim ApplyToRng As Range
Dim wsCondition As Worksheet
' determine the worksheet that define the conditions and formattings
' to do this, create a blank worksheet and name it "Names",
' then in the worksheet,
' column A of the worksheet should contain the names to highlight, start at [A1]
' column B of the worksheet should be filled with the highlight color to apply, working in pair with column A
Set wsCondition = Worksheets("Names")
' i make the Macro to apply to current selection.
' i made it this way so that you can reuse this code on different sheets multiple times
' anyway, you can change this to apply to a fixed range, which can then be turned into automatic running code.
' e.g. Set ApplyToRng = Columns("B")
Set ApplyToRng = Selection
' clear the conditional formattings of current selection. otherwise the list of conditional formatting will keep growing.
ApplyToRng.FormatConditions.Delete
' add the conditions
For Each iRng In wsCondition.Range([A1].Address, wsCondition.Cells(Rows.Count, 1).End(xlUp))
ApplyToRng.FormatConditions.Add Type:=xlTextString, String:=iRng.Value, TextOperator:=XlContainsOperator.xlContains
ApplyToRng.FormatConditions(ApplyToRng.FormatConditions.Count).SetFirstPriority
ApplyToRng.FormatConditions(1).Interior.Color = iRng.Offset(0, 1).Interior.Color
ApplyToRng.FormatConditions(1).StopIfTrue = False
Next iRng
End Sub
The worksheet "Names" would look like this
I would write it as a macro.
Begin with the first sheet.
Find the last used column and the last used row on that sheet.
Use these figures to iterate through each cell in each row.
For each cell you iterate through you need to go to the list and iterate through each item in the list. Compare the cell value and the list value, if they are the same then highlight the row and go the next row.
I hope that helps.

Moving rows to other sheets

Is there a way or formula that could move a certain row to a different sheet by just typing in the criteria and pressing enter?
Like if I have 3 sheets and one of the columns on each sheet was called status, and in this column I type COMP, could it move all the information to a sheet called COMP sheet?
Insert this code on your worksheets
Private Sub Worksheet_Change(ByVal Target As Range)
If (Range("A1").Value = "COMP") Then ' Replace "A1" for your cell that will contain COMP
ActiveSheet.Range("1:1").Copy _ ' Replace 1:1 by your souce row
Destination:=Worksheets("COMP").Range("2:2") 'replace 2:2 by your dest row
End If
End Sub
When the user changes any value in the Worksheet, if the value of "A1" is "COMP", the row you chose (1 in the example) is copied into your destination row (row 2 in the example) in worksheet COMP.
You can build a VBA Macro that does all of the moving for you, but that would require the user to manually run it, not just typing COMP in a cell. You can also have it update automatically without the user pressing COMP and enter by using a formula that references the other worksheets (but then it will always be there).
=Sheet1!A1
The above code will reference the top left cell in the Worksheet 'Sheet1'.

Resources