vba 如果整数,则将单元格的值移动到下一列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24092039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:23:38  来源:igfitidea点击:

Move values of cell to next column if integer

excelvbaformula

提问by Bec

I haven't used formulas in Excel before. Currently, the values of my cells are misaligned. What I would like to do is move the values of column F to column G if they are numerical values. For instance, in the 7th row that is displayed in my image, I'd like to move 48 over to the adjacent cell to the right. Same with 3054, 5770, and 32. However, I DON'T want to move IsCallOnly because it is an alphanumerical value. How would I go about doing this? Thanks!

我以前没有在 Excel 中使用过公式。目前,我的单元格的值未对齐。我想做的是将 F 列的值移动到 G 列(如果它们是数值)。例如,在我的图像中显示的第 7 行中,我想将 48 移到右侧的相邻单元格。与 3054、5770 和 32 相同。但是,我不想移动 IsCallOnly,因为它是一个字母数字值。我该怎么做呢?谢谢!

example of issue

问题示例

回答by user3700285

In cell G2, try:

在单元格 G2 中,尝试:

 =IF(ISNUMBER(F2),F2,"")

This will tell is to copy the adjacent cell if it is a number, but leave the cell blank if it is not.

这将告诉是复制相邻的单元格,如果它是一个数字,但如果不是,则将单元格留空。

回答by pnuts

I suggest, in H1 and copied down:

我建议,在 H1 中复制下来:

=IF(G1="",F1,G1)  

this looks to see if G1 is empty and if it is, take F1, otherwise take G1.

这会查看 G1 是否为空,如果是,则取 F1,否则取 G1。

However, the number values in ColumnF are copied rather than moved, though it could be arranged for 'move' rather than 'copy, if required, with a bit of fiddling.

然而,ColumnF 中的数值是复制而不是移动,尽管它可以安排为“移动”而不是“复制”,如果需要,稍微摆弄一下。

Neither requires VBA but if that is obligatory then it would help if you would post the code you have tried so far.

两者都不需要VBA,但如果这是强制性的,那么如果您发布迄今为止尝试过的代码会有所帮助。

回答by xyz

I think you are asking to move only the numbers in column F to column G?, if so maybe the following will help.

我认为您只是要求将 F 列中的数字移动到 G 列?,如果是这样,以下内容可能会有所帮助。

Sub MoveMyNumbers()

    MoveNumbers "XXX", "F"

End Sub

.

.

Function MoveNumbers(ShtName As String, ColLetter As String)
Dim ws As Excel.Worksheet
Dim ColNumber As Integer
Dim lRow As Long
Dim i As Long

Set ws = ThisWorkbook.Sheets(ShtName)
    lRow = ws.Range(ColLetter & ws.Rows.Count).End(xlUp).Row

   'Get Column Number from column letter
    ColNumber = ws.Range(ColLetter & "1").Column

    For i = 1 To lRow
        If IsNumeric(Cells(i, ColNumber).Value) Then 
           Cells(i, (ColNumber + 1)).Value = Cells(i, ColNumber).Value
           Cells(i, ColNumber).Value = " "
        End IF
    Next i
   End Function