I am Preparing a CSV from Excel. There are few merged cells in excel. Need to make the excel ready for converting to csv file. so need to Un merge excel cells and fill those columns headers repetitive for all cells.Need a MAcro in VBA to achive this
You can use .MergeCells = False look at this example below:
Sub merge_Bouton1_Clic()
With Range("A1:C1")
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("A1").Copy
Range("B1:C1").PasteSpecial xlPasteValues
Application.CutCopyMode = xlCopy
End Sub
[RESULT]:
Related
Hi I'm currently using a macro that autoformats tables for me and aligns all cells centrally except for the ones in the first selected column.
I was wondering if there was a way to tweak this so that the 1st selected column is aligned left only if it contains text and not if it contains a number
Here's the code:
Sub Test_align_left()
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Columns(1).Select
On Error Resume Next
With Selection
.SpecialCells(xlCellTypeConstants, xlTextValues).HorizontalAlignment = xlLeft
.SpecialCells(xlCellTypeFormulas, xlTextValues).HorizontalAlignment = xlLeft
End With
End Sub
Thanks in advance,
Thomas
If you mean left align if text or centred if numeric then here is a way which avoids looping through each cell.
Sub x()
On Error Resume Next
With Columns(1)
.SpecialCells(xlCellTypeConstants, xlTextValues).HorizontalAlignment = xlLeft
.SpecialCells(xlCellTypeConstants, xlNumbers).HorizontalAlignment = xlCenter
.SpecialCells(xlCellTypeFormulas, xlNumbers).HorizontalAlignment = xlCenter
.SpecialCells(xlCellTypeFormulas, xlTextValues).HorizontalAlignment = xlLeft
End With
End Sub
If you just want to leave the first column alone you could do something like:
Sub Test_align_left()
'Test_align_left Macro
With Selection.offset(0,1).resize(,Selection.columns.count-1)
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
End Sub
With my code I want 2 perform formatting over several not adjacent COLUMNS, and I want to make it elegantly in code. Just as it's supposed to be by the book. I'm learning, so I want to learn the right way.
this is how my task looks like:
I've read this.
I mean... is there really no way to enumerate column letters in COLUMNS method? no way to do this?
So do I need to use RANGE object to preform my task? Is it correct?
How do I use UNION method in conjunction with "With... End With"
This is my case I guess.
Please explain more in detail than only 1,5 line answer please.
I will need to do the same things with Columns: B, C, G, H.
They will be formatted .NumberFormat = "#,##0.00"
Option Explicit
Dim VBA As Worksheet
Dim Filter As String
Dim Stock As Variant
Dim Index As Variant
Dim Portfolio As Variant
' Dim Date as Range
Sub Columns_Formatting()
Set VBA = Workbooks("kgh pricing model thursday.xlsm").Worksheets("VBA")
Filter = "Pliki CSV, *.csv," & "Pliki TXT, *.txt," & "All Files, *.*"
' Stock = Application.GetOpenFilename(fileFilter:=Filter, FilterIndex:=1, Title:="Choose file with a stock prices")
' Index = Application.GetOpenFilename(fileFilter:=Filter, FilterIndex:=1, Title:="Choose file with an index values")
' If Stock = False Or Index = False Then MsgBox "Canceled": Exit Sub
With VBA.Columns("A:A,F:F") ' here is the error = type mismath
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.NumberFormat = "yyyy-mm-dd;#"
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
.ColumnWidth = 12
End With
With VBA.Columns("A;F")
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.NumberFormat = "General"
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
.ColumnWidth = 12
End With
With VBA.Range("E2:E" & Rows.Count)
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = True
.NumberFormat = "#,##0"
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
.ColumnWidth = 10
End With
Use a Range object with full column references. You might also want to cut down the full column references to the Worksheet.UsedRange property with the Intersect method.
With VBA
With Intersect(.UsedRange, .Range("A:A,F:F"))
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.NumberFormat = "yyyy-mm-dd;#"
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
.ColumnWidth = 12
End With
End With
I have excel sheet prepared as a data entry sheet called “INPUT SHEET”. Data is added in various columns against fixed certain no. of rows of this “INPUT SHEET”.
At the end of each row I have provided one macro button which picks the value from each column and creates another new sheet.
The problem is that I have 100 such columns and I want to avoid editing each macro to work against each column. I want a single macro which identifies column against which the button is pressed and accordingly works on that column only.
Sample macro for COLUMN U is as below: I want a little modification in this sheet so that same code can be applicable to all coulmns.
' Macro1 Macro===ROW U
'
' Create new sheet copying from DATASHEET 1 before last sheet
'
Worksheets("DATASHEET 1").Copy before:=Sheets(Worksheets.Count)
Set wks = ActiveSheet
Sheets("INPUT").Select
Range("U10").Select
Selection.Copy
' Retaining the name of sheet
'
Range("U150").Select
ActiveSheet.Paste
wks.Name = Range("U10").Value
' Copying the notes
'
Worksheets(Range("u10").Value).Activate
Range("D62:BF87").Select
Selection.ClearContents
Range("AY6").Value = "2"
Range("A7:BF7").Select
ActiveCell.FormulaR1C1 = "=INPUT!R[3]C[20]"
Dim i As Integer, j As Integer
j = 61
For i = 63 To 88
Sheets("INPUT").Select
If Cells(i, 21).Value = "YES" Then
j = j + 1
Worksheets(Range("U10").Value).Activate
Range(Cells(j, 4), Cells(j, 58)).Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Selection.UnMerge
Sheets("INPUT").Select
Cells(i, 2).Copy
Worksheets(Range("U10").Value).Activate
Cells(j, 4).PasteSpecial Paste:=xlPasteValues
Range(Cells(j, 4), Cells(j, 58)).Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
End If
Next i
Rather than having 100 buttons, I would probably work with just one, and move it according to the selected cell. That way each time the cursor moves, the button moves and you could then use the activecell.column method to sum that column.
The code you could use would be something like:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
ActiveSheet.Shapes("Button 1").Left = Cells(40, ActiveCell.Column).Left
End Sub
In the sheet of the workbook you are working on. The button in row 40 will move to the selected column.
Sub ImportFixed()
'
Sheets("Front-Page").Select
Sheets("SPROC").Visible = True
Sheets("SPROC").Select
ThisWorkbook.RefreshALL
DoEvents
'Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
Sheets("SPROC").Select
Range("J2").Select
Range(Selection, Selection.End(xlToLeft)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Master-Data-Sheet").Select
Range("A1914").Select
ActiveSheet.Paste
Sheets("SPROC").Select
Range("N2").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Master-Data-Sheet").Columns("N:N").Range("N1914").Paste
Cells.Select
Application.CutCopyMode = False
With Selection.Font
.Size = 9
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.TintAndShade = 0
End With
With Selection.Font
.Name = "Calibri"
.Size = 9
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
Columns("A:H").Select
With Selection
.HorizontalAlignment = xlGeneral
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Columns("M:N").Select
With Selection
.HorizontalAlignment = xlGeneral
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("L16108").Select
Range("J2105").Select
Range(Selection, Selection.End(xlDown)).Select
Range("J2137").Select
Range("N2137").Select
Sheets("SPROC").Select
ActiveWindow.SelectedSheets.Visible = False
ActiveWindow.ScrollWorkbookTabs Sheets:=-2
Sheets("Master-Data-Sheet").Select
End Sub
I have a report that has a sheet named SPROC. This sheet is refreshed each Monday and pulls through data for that day from a SQL query (any other data on that sheet is overwritten) . What I then want to do is select ALL the data (Columns A:N - The number of rows changes each week so the range isn't fixed) and paste it into the first blank cell in column A on a sheet named Master-Data-Sheet. This second sheet contains ALL the data for previous weeks and is used to populate ALL my pivot tables and graphs etc on various other worksheets. At present I have recorded a Macro but instead of finding the last blank row, it is using a specific range which means that when I run the macro, it overwrites data in the Master Data file. Any Suggestions?
I have included a copy of the VBA code (it also does a lot of other functions so apologies if it is a little long). I think it is lines 20 and 359 where the issue is occurring but I have no idea what to do to fix it (I have tried ALL manner of different variations).
Pretty classical matter, must have a lot of similar question and please get rid of scrolls and things like this in record macros...
try this :
Sub Macro2()
'
Dim ShIn As Worksheet
Dim ShOut As Worksheet
Set ShIn = ThisWorkbook.Sheets("SPROC")
Set ShOut = ThisWorkbook.Sheets("Master-Data-Sheet")
'ShIn.Cells(2, 1).End(xlToRight).Column
Dim RgTotalInput As String
Dim RgTotalOutput As String
RgTotalInput = "$A$2:$" & ColLet(ShIn.Cells(1, 1).End(xlToRight).Column) & "$" & ShIn.Cells(Rows.Count, 1).End(xlUp).Row
RgTotalOutput = "$A$" & ShOut.Cells(Rows.Count, 1).End(xlUp).Row + 1
ShIn.Range(RgTotalInput).Copy Destination:=ShOut.Range(RgTotalOutput)
End Sub
Public Function ColLet(ByVal ColNb As Integer) As String
Dim ColLetTemp As String
Select Case ColNb
Case Is < 27
ColLetTemp = Chr(64 + ColNb)
Case Is > 26
If Int(ColNb / 26) <> ColNb / 26 Then
ColLetTemp = Chr(64 + Int(ColNb / 26)) & Chr(64 + ColNb - 26 * Int(ColNb / 26))
Else
ColLetTemp = Chr(64 + Int(ColNb / 26) - 1) & Chr(64 + 26)
End If
Case Else
End Select
ColLet = ColLetTemp
End Function
I have written this code to merge a few lines in each column, from column C to AZ.
For some reason the range does not match the one I was expecting, in this case the code merges the cells C8:C10 then D8:D12, E8:E12, and so on. lines = 2 in this example.
I don't understand why aren't the ranges matching if lines value is not changing inside the for.
Thanks!
For columns = 0 To 49
Range(Range("C8").Offset(0, columns), Range("C8").Offset((lines), columns)).Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 90
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
next comlumns
Columns is a reserved word. And you said that this code did run?
If I change that to a valid variable then the code runs. The problem is the way you are using Offset
?[C8].offset(2).address after the way you merge will give you $C$12
Also avoid the use of .Select INTERESTING READ And not to mention fully qualify your objects. For example your range and cell objects are not fully qualified and may give you error.
I think, this is what you are trying to achieve?
Sub Sample()
Dim ws As Worksheet
Dim rng As Range
Dim i As Long, rw As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
rw = 2
With ws
For i = 3 To 52
Set rng = .Range(.Cells(8, i), .Cells(8 + rw, i))
With rng
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 90
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Next i
End With
End Sub