Microsoft Excel - Copiar Columnas en una hoja excel a otra con VBA

Asked By Angela M on 18-Nov-11 05:24 AM
Buenos días,

Mi pregunta es la siguiente:
Tengo en la Hoja1 de Excel un archivo con 20 columnas y quisera pasar solo algunas de esas columnas a la Hoja 2 sin que queden columnas en blanco.

Intente escribir el programa en vba para copiar la primera columna y luego desde esa copiar las que quiero que se copien.

Ya me funiono para la columna B, pero quisera saber como hacer para las demas, teniendo en cuenta que las otra no estan siempre juntas. (es decir puede ser columna 3, 5, 9, 11)
Pense hacer con for  next, pero no creo que funicone depronto con find (3,5,9,11), pero no se si esa sea una funcion?
Se podria poner una variable en la aplicacion vlookup? es decir aqui:
Range("B1:B100").Value = Application.WorksheetFunction.VLookup(Range("A1:A100").Value, Worksheets("Hoja1").Range("A:Z"), i)
y que i tenga como valores (3,5,9,11)

Aqui pego el codigo, mil gracias por la ayuda!!!

Sub Copiar()


Dim i As Integer
Dim Num As Integer
i = 1
 
Sheets("Hoja1").Select
Range("A1:A100").Select
Selection.Copy
Sheets("Hoja2").Select
Range("A1:A100").Select
ActiveSheet.Paste
Application.CutCopyMode = False


 Sheets("Hoja2").Select
 Range("B1:B100").Value = Application.WorksheetFunction.VLookup(Range("A1:A100").Value, Worksheets("Hoja1").Range("A:Z"), 3)



End Sub
Anil Kumar replied to Angela M on 18-Nov-11 07:48 AM
Hi Angela,

First of all, not all of us understand Spanish. Please rewrite the question in English. I tries to convert your question into English and read it, but it was not much clear. What exactly you want to achieve, just tell us that.

===========================

Your language converted to english with the code:

My question is as follows: i have in the Sheet1 Excel file with 20 columns and fantastic watch pass only some of those columns to the Sheet 2 without any remaining blank columns.

Try to write the program in vba to copy the first column, and then from that copy and i would like to be copied.

I was merged for the column B, but i would like know how to make to the other, taking into account that the other are not always together. (That is, it can be column 3, 5, 9, 11)

I thought to do with de next, but I do not think that funicone maybe with find (3,5,9,11 ), but i do not know whether this is a function?

We could put a variable in the application vlookup formulas? That is to say here: Range( "B1:B100 " ) .Value = Application.WorksheetFunction.VLookup formulas(Range( "A1:A100 " ) .Value, Worksheets( "Sheet1 " ) .Range( "A:Z" ), (i) and that i have as values (3,5,9,11 )

Here you pasted the code, a thousand thanks for the help!



Sub Copy()

Dim i As Integer
Dim Num As Integer
i = 1
Sheets("sheet1").Select
Range("A1:A100").Select
Selection.Copy
Sheets("sheet2").Select
Range("A1:A100").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Sheets("sheet2").Select
Range("B1:B100").Value = Application.WorksheetFunction.VLookup(Range("A1:A100").Value, Worksheets("sheet1").Range("A:Z"), 3)

End Sub


Do update us again.
Thank you.
Anil
wally eye replied to Angela M on 18-Nov-11 10:26 AM
I'm not completely sure what you want to do, this function will copy the selected columns from the source worksheet to the destination worksheet, if there are any values in them.  Paste this code in a new module:

Public Sub btnCopyNonBlank_Click()
  
  Dim strCols           As String
  Dim arrCols(1 To 4)       As Integer
  
  arrCols(1) = 1
  arrCols(2) = 3
  arrCols(3) = 5
  arrCols(4) = 7
  Call CopyNonBlank(Worksheets("Sheet1"), arrCols, Worksheets("Sheet2"))
  strCols = "1,3,5,7"
  Call CopyNonBlank(Worksheets("Sheet1"), strCols, Worksheets("Sheet2"))
  
End Sub
Public Sub CopyNonBlank(ByVal wksSource As Worksheet, ByVal arrColsIn As Variant, ByVal wksDest As Worksheet)
  
  Dim arrData           As Variant
  Dim arrCols()           As Integer
  
  Dim intPos            As Integer
  Dim intPosLast          As Integer
  Dim intMaxCol           As Integer
  Dim lngLastRow          As Long
  Dim intCol            As Integer
  Dim lngCurrRow          As Long
  Dim intDestCol          As Integer
  
  If TypeName(arrColsIn) = "String" Then
    intPosLast = 0
    intMaxCol = 0
    ReDim arrCols(1 To 1)
    Do
      intPos = InStr(intPosLast + 1, arrColsIn, ",")
      If intPos > 0 Then
        arrCols(UBound(arrCols)) = Mid(arrColsIn, intPosLast + 1, intPos - intPosLast - 1)
        ReDim Preserve arrCols(1 To UBound(arrCols) + 1)
      Else
        arrCols(UBound(arrCols)) = Mid(arrColsIn, intPosLast + 1)
      End If
      If arrCols(UBound(arrCols)) > intMaxCol Then
        intMaxCol = arrCols(UBound(arrCols))
      End If
      intPosLast = intPos
    Loop While intPos > 0
  Else
    ReDim arrCols(LBound(arrColsIn) To UBound(arrColsIn))
    For intPos = LBound(arrColsIn) To UBound(arrColsIn)
      arrCols(intPos) = arrColsIn(intPos)
    Next intPos
  End If
  
  lngLastRow = wksSource.Cells.Find(What:="*", After:=[A1], _
    SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
  arrData = wksSource.Cells(1, 1).Resize(lngLastRow - 1, intMaxCol).Formula
  intDestCol = 0
  For intCol = LBound(arrCols) To UBound(arrCols)
    For lngCurrRow = LBound(arrData) To UBound(arrData)
      If arrData(lngCurrRow, intCol) > "" Then
        intDestCol = intDestCol + 1
        wksSource.Columns(arrCols(intCol)).Copy
        wksDest.Cells(1, intDestCol).PasteSpecial xlValues
        Exit For
      End If
    Next lngCurrRow
  Next intCol
  Application.CutCopyMode = False
  
End Sub

and put a button on your source sheet, have it call btnCopyNonBlank_Click.  I put two versions of the calling routine in, one for using an array of the columns, the second a string with the columns.  You can just delete the one you don't want.

The btnCopyNonBlank_Click function sets up the variables for the CopyNonBlank function.  CopyNonBlank first sets up an array holding the desired column numbers, put the entire source sheet into an array, then loops through each of the columns looking for data.  If it finds data in the column, then it copies the entire column to the next column in the destination worksheet.

Let me know if this works for you, or if you need some tweaks.