Posts

Showing posts with the label Vba

Copy Paste Values Only( XlPasteValues )

Answer : If you are wanting to just copy the whole column, you can simplify the code a lot by doing something like this: Sub CopyCol() Sheets("Sheet1").Columns(1).Copy Sheets("Sheet2").Columns(2).PasteSpecial xlPasteValues End Sub Or Sub CopyCol() Sheets("Sheet1").Columns("A").Copy Sheets("Sheet2").Columns("B").PasteSpecial xlPasteValues End Sub Or if you want to keep the loop Public Sub CopyrangeA() Dim firstrowDB As Long, lastrow As Long Dim arr1, arr2, i As Integer firstrowDB = 1 arr1 = Array("BJ", "BK") arr2 = Array("A", "B") For i = LBound(arr1) To UBound(arr1) Sheets("Sheet1").Columns(arr1(i)).Copy Sheets("Sheet2").Columns(arr2(i)).PasteSpecial xlPasteValues Next Application.CutCopyMode = False End Sub since you only want values copied, you can pass the values of arr1 directly to arr2 and avoid c...

AndAlso/OrElse In VBA

Answer : The only short circuiting (of a sort) is within Case expression evaluation, so the following ungainly statement does what I think you're asking; Select Case True Case (myObject Is Nothing), Not myObject.test() MsgBox "no instance or test == false" Case Else MsgBox "got instance & test == true" End Select End Sub This is an old question, but this issue is still alive and well. One workaround I've used: Dim success As Boolean ' False by default. If myObj Is Nothing Then ' Object is nothing, success = False already, do nothing. ElseIf Not myObj.test() Then ' Test failed, success = False already, do nothing. Else: success = True ' Object is not nothing and test passed. End If If success Then ' Do stuff... Else ' Do other stuff... End If This basically inverts the logic in the original question, but you get the same result. I think it's a cleaner solution t...

Add Newline To VBA Or Visual Basic 6

Answer : Visual Basic has built-in constants for newlines: vbCr = Chr$(13) = CR (carriage-return character) - used by Mac OS and Apple II family vbLf = Chr$(10) = LF (line-feed character) - used by Linux and Mac OS X vbCrLf = Chr (13) & Chr (10) = CRLF (carriage-return followed by line-feed) - used by Windows vbNewLine = the same as vbCrLf Use this code between two words: & vbCrLf & Using this, the next word displays on the next line. There are actually two ways of doing this: st = "Line 1" + vbCrLf + "Line 2" st = "Line 1" + vbNewLine + "Line 2" These even work for message boxes (and all other places where strings are used).