简体   繁体   English

如何将值从单元格复制到各种单元格VBA Excel

[英]How to copy a value from a cell to various cells VBA Excel

I will like to do something like this using VBA in Excel. 我想在Excel中使用VBA做类似的事情。 I need to copy each value in column A(text or numeric) and the format of the cell to the column B twice . 我需要将A列中的每个值(文本或数字)和单元格的格式复制到B列两次。 The result should be like this : 结果应该是这样的:

在此处输入图片说明 How can this be done in VBA? 如何在VBA中完成此操作?

There are probably a dozen different answers to this question as you are not being very precise. 由于您不太精确,因此可能有十二个不同的答案。 Yet, this is one possible answer: 但是,这是一个可能的答案:

Option Explicit

Public Sub Test()
Dim lngRow As Long

For lngRow = 1 To 4
    Worksheets("Sheet1").Cells(lngRow, 1).Formula = "=row()"
    Worksheets("Sheet1").Cells(lngRow + lngRow - 1, 2).Formula = "=A" & lngRow
    Worksheets("Sheet1").Cells(lngRow + lngRow, 2).Formula = "=A" & lngRow
Next lngRow

End Sub

I did it like this: 我这样做是这样的:

Private Sub CommandButton1_Click() Dim ws As Worksheet 专用子CommandButton1_Click()昏暗的工作表

For Each ws In ThisWorkbook.Worksheets 对于ThisWorkbook.Worksheets中的每个ws

Dim c As Range
For Each c In ws.UsedRange.Columns("A").Cells
ActiveSheet.Cells(c.Value, 1).Formula = "=row()"
ActiveSheet.Cells(c.Value + c.Value - 1, 2).Formula = "=A" & c.Value
ActiveSheet.Cells(c.Value + c.Value, 2).Formula = "=A" & c.Value

Next c Next ws 下一步c下一步ws

End Sub 结束子

But if I put a non-numeric value in the first column it does not work. 但是,如果我在第一列中放置一个非数字值,它将无法正常工作。 What I am missing? 我缺少什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VBA | 如何在Excel中将值从单元格复制到单元格 - VBA | How to Copy value from Cell to Cell in Excel Excel VBA-如果单元格值满足条件,则在工作表之间复制单元格 - Excel VBA - Copy cells between sheets if cell value satisfies criteria Excel VBA如何将WorkSheet名称与单元格值匹配,然后复制并粘贴一系列单元格 - Excel VBA How to match WorkSheet name with Cell value, then Copy and Paste a Range of Cells 从各种工作簿的各个单元格复制Excel VBA - Excel VBA Copying from Various cells from various Workbooks Excel VBA从列复制值并将值粘贴到单元格中 - excel vba copy value from a column and paste value in a cell 如果> 0,则Excel VBA将单元格值复制到左侧的单元格 - Excel VBA to copy cell value if > 0 to cell to the left Excel VBA:在特定单元格中设置值,然后复制一组单元格并粘贴值,然后迭代该值 - Excel VBA: Set a value in a specific cell, then copy a set of cells and paste values, then iterate the value Excel VBA:将单元格值从一个工作簿复制到另一个? - excel vba: copy cell value from one workbook to another? EXCEL VBA-遍历一行中的单元格,如果不为空,则将单元格值复制到另一行,直到下一行不为空 - EXCEL VBA - Loop through cells in a row, if not empty, copy cell value into another row until next row not empty Excel VBA宏可根据列中的文本将单元格值复制并粘贴到某些单元格 - Excel VBA Macros to copy and paste cell value to certain cells based on text in column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM